python中的map、filter、reduce函数

来源:互联网 发布:钢铁雄心4汉化版mac 编辑:程序博客网 时间:2024/05/18 14:44
python中有几个常用的函数工具,它们包括map、filter、reduce,本文主要讲解这3个函数的用法。

1.map函数
首先看一下map函数的帮助文档:
>>> 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.
 |  
 |  Methods defined here:
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  __next__(self, /)
 |      Implement next(self).
 |  
 |  __reduce__(...)
 |      Return state information for pickling.
从说明中可以看到,map函数接受两个参数,第一个为一个函数,第二个为一个或多个iterable的列表。假设iteratables这个tuple中有n个iteratable对象,它的作用是对这n个iteratable对象同时进行遍历,每个iteratable中得到一个元素,将这个n个元素传入func中进行计算。map函数返回一个generator。
说了这么多可能有点拗口,下面来看两个例子便立刻明白了:
>>> li = [1,2,3,4]
>>> li = list(map(lambda x :x * x, li))
>>> li
[1, 4, 9, 16]
>>> 
以上的map函数负责将li中的每个元素取平方,然后返回。
而以下例子,
>>> t = (1,2,3,4,5)
>>> li = list(map(lambda x, y: x + y, li, t))
>>> li
[2, 6, 12, 20]
>>> 
以上map函数的调用中,传递了两个iterable的对象(一个list,一个tuple)到iterables参数中,因此iterables这个tuple包含了两个iterable对象;map方法中的func参数lambda表达式接受两个值,其中x和y分别对应list对象中的元素和tuple对象中的元素。这里的调用效果是对两个iterable对象中对应位置处的元素进行求和。map函数的迭代会在其中最短的那个iterable对象的最后一个元素处理完之后结束,这里的t有5个元素,但是li只有4个元素,因此在处理完li的最后一个元素之后,map的迭代便结束了。

2.filter函数
同样,在正式介绍filter之前,先看一下filter的帮助文档:
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.
 |  
 |  Methods defined here:
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  __next__(self, /)
 |      Implement next(self).
 |  
 |  __reduce__(...)
 |      Return state information for pickling.
从接口中可以看到,filter第一个参数为一个函数,第二个参数为一个iterable的对象。它用于对iterable对象中的元素进行过滤,其中func指定了过滤条件,只有func(x)返回true的元素x才会使用,其他的被丢弃。返回值与map的类似,这里也返回了一个generator。
例如,
li = list(range(1,10))
fi = filter(lambda x : (x % 2) == 0, li)
以上的filter用于取li中的所有偶数。

3.reduce函数
reduce函数在functools这个模块中,看到这个函数名称就很容易让人想起Map_Reduce。这个函数其实跟Map_Reduce中的reduce步骤的工作是相同的,它负责将一系列的值按照某种规则进行整合,最终得到一个值。首先来看看reduce的帮助文档:
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的用法也就一目了然了。这里不做更多的介绍。

0 0
原创粉丝点击