python高级函数-map、filter、reduce区别

map函数

map(function, iterable, …)

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. For cases where the function inputs are already arranged into argument tuples, see itertools.starmap().

返回一个迭代器,它将函数应用于可迭代的每一项,从而产生结果。如果传递了额外的iterable参数,那么函数必须接受许多参数,并且并行地应用于所有iterables的项目。有了多个iterables,迭代器就会在最短的迭代器耗尽时停止。对于那些已经将函数输入安排成参数元组的情况,请参见itertools.starmap()

遍历序列,对序列中每个元素进行操作,最终获取新的序列的生成器对象

def square(x):
    return x**2
result = map(square,[1,2,3,4,5])
print(result)
输出结果:
<map object at 0x006F34F0>

reduce函数

functools.reduce(function, iterable[, initializer])

Apply function of two arguments cumulatively to the items of 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). The left argument, x, is the accumulated value and the right argument, y, is the update value from the sequence. If the optional initializer is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty. If initializer is not given and sequence contains only one item, the first item is returned.

将两个参数的函数累加到序列项中,从左到右,以便将序列减少为单个值。 例如,reduce(lambda x,y:x + y,[1,2,3,4,5])结果(((((1 + 2)+3)+4)+5)。 左参数x是累加值,右参数y是序列的更新值。 如果存在可选的初始值设定项,则它将位于计算中序列的项之前,并在序列为空时用作默认值。 如果未给出初始化程序且序列仅包含一个项目,则返回第一个项目。

Roughly equivalent to:

def reduce(function, iterable, initializer=None):
    it = iter(iterable)
    if initializer is None:
        value = next(it)
    else:
        value = initializer
    for element in it:
        value = function(value, element)
    return value

对于序列内所有元素进行累计操作,即是序列中后面的元素与前面的所有元素做累积计算(结果是所有元素共同作用的结果)

def square(x,y):
     return x*y
result = reduce(square,range(1,5))
print(result)
输出结果:
24

filter函数

filter(function, iterable)

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.

从iterable的那些元素构造一个迭代器,函数返回true。 iterable可以是序列,支持迭代的容器,也可以是迭代器。 如果function为None,则假定为identity函数,即删除所有可迭代的false元素。

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.

请注意,如果函数不是None,则filter(function,iterable)等效于生成器表达式(如果function(item),则为iterable中的item项),如果function为None,则(如果是item,则为item中的item项)。

See itertools.filterfalse() for the complementary function that returns elements of iterable for which function returns false.

请参阅itertools.filterfalse()以获取补充函数,该函数返回函数返回false的iterable元素。

‘筛选函数’,filter()把传入的函数依次作用于序列的每个元素,然后根据返回值是True还是false决定保留还是丢弃该元素,返回符合条件的序列

def func(x):
    return x%2==0

print(filter(func,range(1,6)))  # 返回结果是生辰器
print(list(filter(func,range(1,6))))
输出结果:
<filter object at 0x0000024E25526278>
[2, 4]

猜你喜欢

转载自blog.csdn.net/lb786984530/article/details/81184799