高阶函数map(),filter(),reduce()

接受函数作为参数,或者把函数作为结果返回的函数是高阶函数,官方叫做 Higher-order functions。

map()和filter()是内置函数。在python3中,reduce()已不再是内置函数,被放到了functools模块里面,这个函数最常用于求和。

另外,列表推导式和生成器表达式具有map()和filter()两个函数的功能,而且更易于阅读。


map()

在python3中,map()函数返回的是一个可迭代的map对象,可用list()函数转换为列表。

map()函数将参数序列中的元素传递给参数函数,然后将生成的结果返回组成新的可迭代对象。

map()函数可传递多个参数序列,运行方式与zip()函数类似,这里不再细说。

>>> help(map)
Help on class map in module builtins: class map(object)
| map(func, *iterables) --> map object
|
| Make an iterator that computes the function using arguments from each of the iterables. Stops when the shortest iterable is exhausted.
Return an iterator that applies function to every item of iterable, yielding the results.
If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted.
>>> map(lambda x: x ** 2, [1, 2, 3, 4, 5])
<map object at 0x0000000002E86208>
>>> list(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])
<map object at 0x0000000002E86208>
>>> list(map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10]))
[3, 7, 11, 15, 19]

filter()

在python3中,filter()函数返回的是一个可迭代的filter对象,同样可用list()函数转换为列表。

filter()函数用来过滤序列,参数函数为判断函数,参数序列中判断为真的元素返回组成新的可迭代对象。

>>> help(filter)
Help on class filter in module builtins: class filter(object)
| filter(function or None, iterable) --> filter object
|
| Return an iterator yielding those items of iterable for which function(item) is true. If function is None, return the items that are true.
Construct an iterator from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.

Note that filter(function, iterable) is equivalent to the generator expression (item for item in iterable if function(item)) if function is not None and (item for item in iterable if item) if function is None.
>>> filter(lambda x: x % 3, range(20))
<filter object at 0x0000000002B7DBE0>
>>> list(filter(lambda x: x % 3, range(20)))
[1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19]

reduce()

reduce()函数会对参数序列中的元素从左到右进行累积,最终reduce为单个value。

reduce()函数直接返回value,而不是迭代器。

>>> from functools import reduce

>>> help(reduce)
Help on built-in function reduce in module _functools: 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()函数的参数函数必须跟两个参数,完成从左到右累积。如果不是两个参数,会报 TypeError 异常。

>>> reduce(lambda x: x + 1, [1,2,3,4,5])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: <lambda>() takes 1 positional argument but 2 were given

参考:

https://docs.python.org/3/library/functools.html

https://docs.python.org/3/library/functions.html#map

https://docs.python.org/3/library/functions.html#filter

https://docs.python.org/3/library/functools.html#functools.reduce

上一篇:PHPStorm设置调试


下一篇:数据库(数据库、表及表数据、SQL语句)