Python内置函数map与reduce用法简介

map与reduce是两个十分常用的Python内置函数,它们与Hadoop中的MapReduce在某些方面有一定的相似之处。

map函数:

map(function, iterable, ...)

对于可迭代对象(iterable)中的每一个元素调用处理函数(function),并以列表(list)形式返回每个元素的调用结果。如果传递了不止一个可迭代对象参数,函数从各个可迭代对象中取出相同位置的元素加以并行处理。如果可迭代对象长短不一,则为较短的参数末尾补充None元素,使其长度补齐。如果处理函数为None,则视为恒等函数(identity function,返回值等于传入参数的函数叫做恒等函数);如果传入了多个参数,map()会返回一个包含各个可迭代对象对应结果的元组列表(可以视为转置操作)。可迭代参数可以是一个序列(sequence),或者任何可以迭代的对象;结果总是返回list。

Apply function to every item of iterable and return a list of 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. If one iterable is shorter than another it is assumed to be extended with None items. If function is None, the identity function is assumed; if there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation). The iterable arguments may be a sequence or any iterable object; the result is always a list.

map函数示例:

基本用法:

>>> map(lambda x : x*2, [1, 2, 3, 4, 5])
[2, 4, 6, 8, 10]

传递多个iterable对象:

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

传递多个长短不一的iterable对象:

>>> map(lambda x, y : str(x)+str(y), ['a', 'e', 'i'], ['o', 'u'])
['ao', 'eu', 'iNone']

function为None:

>>> map(None, [1, 2, 3, 4, 5])
[1, 2, 3, 4, 5]

function为None,且传递多个iterable对象:

>>> map(None, [1, 3, 5], [2, 4, 6])
[(1, 2), (3, 4), (5, 6)]

reduce函数:

reduce(function, iterable[, initializer])

对可迭代对象(iterable)从左向右累积地调用处理函数(function),从而将可迭代对象缩减为单个值。例如,reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) 等价于计算 ((((1+2)+3)+4)+5)。 左边的参数x是被累积的值, 右边的参数y是从迭代对象更新的值。如果传入了可选的初始值(initializer),它会置于迭代对象中的元素参与运算之前,并且在可迭代对象为空时充当缺省值。如果没有提供初始值,并且可迭代对象只包含单个元素时,返回第一个元素。大致等价于下面的代码:

Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the iterable. If the optional initializer is present, it is placed before the items of the iterable in the calculation, and serves as a default when the iterable is empty. If initializer is not given and iterable contains only one item, the first item is returned. Roughly equivalent to:

def reduce(function, iterable, initializer=None):
    it = iter(iterable)
    if initializer is None:
        try:
            initializer = next(it)
        except StopIteration:
            raise TypeError('reduce() of empty sequence with no initial value')
    accum_value = initializer
    for x in it:
        accum_value = function(accum_value, x)
    return accum_value

reduce函数示例:

基本用法:

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

传入初始值:

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

传入初始值,iterable对象为空列表:

>>> reduce(lambda x, y: x+y, [], 6)
6

传入只包含一个元素的iterable对象:

>>> reduce(lambda x, y: x+y, [1])
1

当初始值与可迭代对象同时为空时,会抛出下面的错误提示:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: reduce() of empty sequence with no initial value

本文链接:http://bookshadow.com/weblog/2015/08/05/python-built-in-functions-map-reduce/
请尊重作者的劳动成果,转载请注明出处!书影博客保留对文章的所有权利。

如果您喜欢这篇博文,欢迎您捐赠书影博客: ,查看支付宝二维码