1. filter(function, sequence)
Do function(item) for the item of sequence
return the item whose result is True.
def f(x):
return x % 3 != 0 and x % 4 != 0
filter(f, range(2, 25))
Out[34]: [2, 5, 7, 10, 11, 13, 14, 17, 19, 22, 23]
2. map(function, sequence, [sequence])
Do function(item) for the item of sequence
return a list with the excute result
def f(x):
return x*x*x
map(f, range(1, 15))
Out[35]: [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000, 1331, 1728, 2197, 2744]
def f(x, y):
return x+y
map(f, range(9), range(9))
Out[36]: [0, 2, 4, 6, 8, 10, 12, 14, 16]
3. reduce(function, sequence, starting_value)
def f(x,y):
return x+y
reduce(f, range(1, 15))
Out[38]: 105
reduce(f, range(1, 15), 20)
Out[39]: 125
4. lambda
f = lambda x: x * 3
f(3)
or
(lambda x: x * 3)(3)
Out[42]: 9
5. lambda & reduce, lambda & map
map(lambda x, y: x + y, [1,3,5], [2,4,6])
Out[44]: [3, 7, 11]
map(lambda x, y: (x+y, x-y), [1,3,5], [2,4,6])
Out[45]: [(3, -1), (7, -1), (11, -1)]
reduce((lambda x,y: x+y), range(5))
Out[46]: 10