本文参考文章:http://blog.sina.com.cn/s/blog_45ac0d0a010191rb.html,作者lambda47的博客
1、map函数

map(...)
map(function, sequence[, sequence, ...]) -> list

Return a list of the results of applying the function to the items of
the argument sequence(s). If more than one sequence is given, the
function is called with an argument list consisting of the corresponding
item of each sequence, substituting None for missing values when not all
sequences have the same length. If the function is None, return a list of
the items of the sequence (or a list of tuples if more than one sequence).

>>> map(lambda x: x ** 2, [1, 2, 3, 4, 5])
[1, 4, 9, 16, 25]

>>> map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
[3, 7, 11, 15, 19]

如果集合长度不相等,会以最小长度对所有集合进行截取。

当函数为None时,操作和zip相似:

>>> map(None, [1])
[1]

>>> map(None, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
[(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]

>>> map(None, [1, 3, 5, 7, 9])
[1, 3, 5, 7, 9]

2、filter

filter函数会对指定序列执行过滤操作。

>>> help(filter)
Help on built-in function filter in module __builtin__:

filter(...)
filter(function or None, sequence) -> list, tuple, or string

Return those items of sequence for which function(item) is true. If
function is None, return the items that are true. If sequence is a tuple
or string, return the same type, else return a list.

>>> filter(None, [1,2])
[1, 2]

>>> filter(lambda x: x&1!=0, [1, 2, 3, 4, 5])
[1, 3, 5]

 

3.reduce函数
reduce函数,reduce函数会对参数序列中元素进行累积。
function参数是一个有两个参数的函数,reduce依次从sequence中取一个元素,和上一次调用function的结果做参数再次调用function。
第一次调用function时,如果提供initial参数,会以sequence中的第一个元素和initial作为参数调用function,否则会以序列sequence中的前两个元素做参数调用function。

reduce(...)
reduce(function, sequence[, initial]) -> value

Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.

>>> reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
15

>>> reduce(lambda x,y:x+y,range(1,11))
55