Python3版本中的filter函数,map函数和reduce函数

一.filter函数:

  filter()为已知的序列的每个元素调用给定的布尔函数,调用中,返回值为非零的元素将被添加至一个列表中

 1 def f1(x):  
 2     if x>20: 
 3         return True  
 4     else:  
 5         return False  
 6   
 7 l1 = [ 1, 2, 3, 42, 67, 16 ]  
 8 print(filter(f1, l1))  
 9 #输出如下:
10 #<filter object at 0x000000000117B898>  
11 l2 = filter( f1, l1 )  
12 print(l2)
13 #输出如下  
14 # <filter object at 0x0000000000BCB898>  
15 print(l2.__next__)
16 #输出如下  
17 # <method-wrapper '__next__' of filter object at 0x000000000074B898>  
18 print(l2.__next__())  
19 # 42  
20 print(l2.__next__())  
21 # 67  
22 print(l2.__next__())
23 #遍历结束出现异常  
24 # Traceback (most recent call last):  
25 #   File "<pyshell#14>", line 1, in <module>  
26 #     l2.__next__()  
27 # StopIteration  

二.map函数:

  map()将函数调用映射到每个序列的对应元素上并返回一个含有所有返回值的列表

 1 def f1( x, y ):  
 2     return (x,y)  
 3   
 4 l1 = [ 0, 1, 2, 3, 4, 5, 6 ]  
 5 l2 = [ 'Sun', 'M', 'T', 'W', 'T', 'F', 'S' ]   
 6 l3 = map( f1, l1, l2 )  
 7 print(list(l3))  
 8 #[(0, 'Sun'), (1, 'M'), (2, 'T'), (3, 'W'), (4, 'T'), (5, 'F'), (6, 'S')]  
 9 
10 def f2(x):
11     return(x*2)  
12 print( list(map(f2, l1)))  
13 #[0, 2, 4, 6, 8, 10, 12]  
14   
15 print( list(map(f2, l2)))  
16 # ['SunSun', 'MM', 'TT', 'WW', 'TT', 'FF', 'SS']  
17   
18 def f3( x, y ):  
19     return (x*2, y*2)  
20   
21 print( list(map(f3, l1, l2)))  
22 # [(0, 'SunSun'), (2, 'MM'), (4, 'TT'), (6, 'WW'), (8, 'TT'), (10, 'FF'), (12, 'SS')] 

三.reduce函数:

  在Python 3里,reduce()函数已经被从全局名字空间里移除了,它现在被放置在fucntools模块里 用的话要 先引 入:

1 from functools import reduce 
2 l1=[0, 1, 2, 3, 4, 5, 6]
3 def f4(x,y):
4     return(x+y)
5 print(reduce( f4, l1 ))
6 #21

四.三个函数的总结:

  三个函数比较类似,都是应用于序列的内置函数。常见的序列包括list、tuple、str。

1.map函数

  map函数会根据提供的函数对指定序列做映射。

  map函数的定义:

    map(function, sequence[, sequence, ...]) ---> list

  通过定义可以看到,这个函数的第一个参数是一个函数,剩下的参数是一个或多个序列,返回值是一个集合。 function可以理解为是一个一对一或多对一函数,map的作用是以参数序列中的每一个元素调用function函数,返回包含每次function函数返回值的list。

  比如要对一个序列中的每个元素进行平方运算:

    map(lambda x: x ** 2, [1, 2, 3, 4, 5])

  返回结果为:

    [1, 4, 9, 16, 25]

  在参数存在多个序列时,会依次以每个序列中相同位置的元素做参数调用function函数。

  比如要对两个序列中的元素依次求和。

    map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])

  map返回的list中第一个元素为,参数序列1的第一个元素加参数序列2中的第一个元素(1 + 2),

  list中的第二个元素为,参数序列1中的第二个元素加参数序列2中的第二个元素(3 + 4), 依次类推,

  最后的返回结果为: [3, 7, 11, 15, 19]

  要注意function函数的参数数量,要和map中提供的集合数量相匹配。 如果集合长度不相等,会以最小长度对所有集合进行截取。 当函数为None时,操作和zip相似:       map(None, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])   #这个是python2中的方法了,py3中不能传递None的类型

    返回结果为:

      [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]

2.filter函数

  filter函数会对指定序列执行过滤操作。

  filter函数的定义:

    filter(function or None, sequence) ----> list, tuple, or string

  function是一个谓词函数,接受一个参数,返回布尔值True或False。

  filter函数会对序列参数sequence中的每个元素调用function函数,最后返回的结果包含调用结果为True的元素。

  返回值的类型和参数sequence的类型相同

  比如返回序列中的所有偶数:

  

1 def is_even(x): 
2     return x%2 == 0 
3 print(list(filter(is_even, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])))
4 # [2, 4, 6, 8, 10]
5 # [Finished in 0.1s]

  如果function参数为None,返回结果和sequence参数相同。

3.reduce函数

  reduce函数,reduce函数会对参数序列中元素进行累积。

  reduce函数的定义:

    reduce(function, sequence[, initial]) -----> value

  function参数是一个有两个参数的函数,reduce依次从sequence中取一个元素,和上一次调用function的结果做参数再次调用function。 第一次调用function时,如果提供initial参数,会以sequence中的第一个元素和initial作为参数调用function,否则会以序列sequence中的前两个元素做参数调用function。

  

1 from functools import reduce
2 print(reduce(lambda x, y: x + y, [2, 3, 4, 5, 6], 1)) 
3 #结果为21( (((((1+2)+3)+4)+5)+6) ) 
4 print(reduce(lambda x, y: x + y, [2, 3, 4, 5, 6])) 
5 #结果为20

  注意function函数不能为None。

原文地址:https://www.cnblogs.com/AlwaysWIN/p/6196350.html