中级篇-内置函数 (map/filter/reduce)

1、map()

 1 array=[1,3,4,71,2]
 2 
 3 ret=[]
 4 for i in array:
 5     ret.append(i**2)
 6 print(ret)
 7 
 8 #如果我们有一万个列表,那么你只能把上面的逻辑定义成函数
 9 def map_test(array):
10     ret=[]
11     for i in array:
12         ret.append(i**2)
13     return ret
14 
15 print(map_test(array))
16 
17 #如果我们的需求变了,不是把列表中每个元素都平方,还有加1,减一,那么可以这样
18 def add_num(x):
19     return x+1
20 def map_test(func,array):
21     ret=[]
22     for i in array:
23         ret.append(func(i))
24     return ret
25 
26 print(map_test(add_num,array))
27 #可以使用匿名函数
28 print(map_test(lambda x:x-1,array))
29 
30 
31 #上面就是map函数的功能,map得到的结果是可迭代对象
32 print(list(map(lambda x:x-1,range(5))))
  print(list(map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])))


33 map函数

结论:
map() 会根据提供的函数对指定序列做映射

 map() 函数语法:

map(function, iterable, ...)


 

2、filter()

 1 #电影院聚集了一群看电影bb的傻逼,让我们找出他们
 2 movie_people=['alex','wupeiqi','yuanhao','sb_alex','sb_wupeiqi','sb_yuanhao']
 3 
 4 def tell_sb(x):
 5     return x.startswith('sb')
 6 
 7 
 8 def filter_test(func,array):
 9     ret=[]
10     for i in array:
11         if func(i):
12             ret.append(i)
13     return ret
14 
15 print(filter_test(tell_sb,movie_people))
16 
17 
18 #函数filter,返回可迭代对象
19 print(filter(lambda x:x.startswith('sb'),movie_people))
20 
21 filter函数

 

3、reduce()

 1 from functools import reduce
 2 #合并,得一个合并的结果
 3 array_test=[1,2,3,4,5,6,7]
 4 array=range(100)
 5 
 6 #报错啊,res没有指定初始值
 7 def reduce_test(func,array):
 8     l=list(array)
 9     for i in l:
10         res=func(res,i)
11     return res
12 
13 # print(reduce_test(lambda x,y:x+y,array))
14 
15 #可以从列表左边弹出第一个值
16 def reduce_test(func,array):
17     l=list(array)
18     res=l.pop(0)
19     for i in l:
20         res=func(res,i)
21     return res
22 
23 print(reduce_test(lambda x,y:x+y,array))
24 
25 #我们应该支持用户自己传入初始值
26 def reduce_test(func,array,init=None):
27     l=list(array)
28     if init is None:
29         res=l.pop(0)
30     else:
31         res=init
32     for i in l:
33         res=func(res,i)
34     return res
35 
36 print(reduce_test(lambda x,y:x+y,array))
37 print(reduce_test(lambda x,y:x+y,array,50))
38 
39 reduce函数

 

4、总结

# map() ---对列表元素统一处理,列表的元素不变,处理方法可以用隐函数lambda
array =[1,3,4,71,2]
print(list(map(lambda x:x+1,array)))

# filter() ---对列表元素过滤掉不符合的,留下符合条件的元素组成新的列表
name=["a-sb","b","c","d-sb"]
print(list(filter(lambda x:x.endswith('sb'),name)))
print(list(filter(lambda x:not x.endswith('sb'),name)))

#reduce() ---把列表变成一个值,处理方法隐函数lambda
from functools import reduce
array =[1,3,4,10,2]
print(reduce(lambda x,y:x*y,array))

 

 

 1 #当然了,map,filter,reduce,可以处理所有数据类型
 2 
 3 name_dic=[
 4     {'name':'alex','age':1000},
 5     {'name':'wupeiqi','age':10000},
 6     {'name':'yuanhao','age':9000},
 7     {'name':'linhaifeng','age':18},
 8 ]
 9 #利用filter过滤掉千年王八,万年龟,还有一个九千岁
10 def func(x):
11     age_list=[1000,10000,9000]
12     return x['age'] not in age_list
13 
14 
15 res=filter(func,name_dic)
16 for i in res:
17     print(i)
18 
19 res=filter(lambda x:x['age'] == 18,name_dic)
20 for i in res:
21     print(i)
22 
23 
24 #reduce用来计算1到100的和
25 from functools import reduce
26 print(reduce(lambda x,y:x+y,range(100),100))
27 print(reduce(lambda x,y:x+y,range(1,101)))
28 
29 #用map来处理字符串列表啊,把列表中所有人都变成sb,比方alex_sb
30 name=['alex','wupeiqi','yuanhao']
31 
32 res=map(lambda x:x+'_sb',name)
33 for i in res:
34     print(i)

 

posted @ 2020-05-29 11:01  zhuanfang  阅读(123)  评论(0编辑  收藏  举报