十一、python沉淀之路--map函数、filter函数、reduce函数、匿名函数、内置函数

一、map函数

1、自定义函数,实现类似于map函数的功能

 1 num_l = [1,3,4,5,6,9]
 2 def power(n):
 3     return n ** 2
 4 def map_test(func,array):
 5     li0 = []
 6     for i in array:
 7         p = func(i)
 8         li0.append(p)
 9     return li0
10 
11 f = map_test(power,num_l)  运用自己定义的函数来计算
12 print(f)
13 f = map_test(lambda x: x ** 2, num_l)   #调用匿名函数实现简单的功能,减少代码量,以下几种类似
14 print(f)
15 
16 
17 def add_one(n):
18     return n + 1
19 
20 f1 = map_test(add_one,num_l)
21 print(f1)
22 f1 = map_test(lambda x: x+1, num_l)
23 print(f1)
24 
25 def reduce_one(n):
26     return n - 1
27 
28 f2 = map_test(reduce_one,num_l)
29 print(f2)
30 f2 = map_test(lambda x: x - 1, num_l)
31 print(f2)
1 [1, 9, 16, 25, 36, 81]
2 [1, 9, 16, 25, 36, 81]
3 [2, 4, 5, 6, 7, 10]
4 [2, 4, 5, 6, 7, 10]
5 [0, 2, 3, 4, 5, 8]
6 [0, 2, 3, 4, 5, 8]

2、map函数的运用:作用于成哥序列,让整个序列实现想要的转换

 1 ############n内置函数 map 的使用
 2 num_l = [1,3,4,5,6,9]
 3 f3 = map(lambda x:x + 3, num_l)  # map(func, *iterables) --> map object  这是map函数官方解释
 4 print(f3)
 5 print(list(f3))    #注意细节:map返回只是一个object,需要用list形式打印出来
 6 
 7 s = 'abcefg'
 8 f4 = map(lambda st:st.upper(),s)
 9 print(f4)
10 print(list(f4))
1 <map object at 0x000001C6AC2B7860>
2 [4, 6, 7, 8, 9, 12]
3 <map object at 0x000001C6AC2B7898>
4 ['A', 'B', 'C', 'E', 'F', 'G']

二、filter函数

1、自定义函数,实现类似于filter的功能

例1:铺垫

 1 bjper = ['bj_老王','bj_老赵','bj_老李','tian an men','gugong']
 2 def filter_test(array):
 3     li0 = []
 4     li1 = []
 5     for i in array:
 6         if i.startswith('bj'):
 7             li0.append(i)
 8         if not i.startswith('bj'):
 9             li1.append(i)
10     return li0,li1
11 
12 f = filter_test(bjper)
13 print(f)
1 (['bj_老王', 'bj_老赵', 'bj_老李'], ['tian an men', 'gugong'])

例2

 1 def show_bj(s):
 2     return s.startswith('bj')
 3 
 4 bjper = ['bj_老王ha','bj_老赵','bj_老李','tian an menha','gugongha']
 5 def filter_test(func,array):
 6     li0 = []
 7     for i in array:
 8         if func(i):
 9             li0.append(i)
10     return li0
11 
12 f = filter_test(show_bj,bjper)
13 print(f)
14 # lambad 运用
15 f = filter_test(lambda s:s.endswith('ha'),bjper)
16 print(f)
1 ['bj_老王ha', 'bj_老赵', 'bj_老李']
2 ['bj_老王ha', 'tian an menha', 'gugongha']

2、filter函数运用:主要筛选出想要的元素

 1 ################ filter 应用:官方解释:filter(function or None, iterable) --> filter object
 2 bjper = ['bj_老王ha','bj_老赵','bj_老李','tian an menha','gugongha']
 3 def show_bj(s):
 4     return s.startswith('bj')
 5 f1 = filter(show_bj,bjper)
 6 print(f1)
 7 print(list(f1))                  #注意细节:filter返回只是一个object,需要用list形式打印出来
 8 
 9 f2 = filter(lambda st: not st.endswith('ha'),bjper)
10 print(f2)
11 print(list(f2))
1 <filter object at 0x00000218E63A7898>
2 ['bj_老王ha', 'bj_老赵', 'bj_老李']
3 <filter object at 0x00000218E63A78D0>
4 ['bj_老赵', 'bj_老李']

三、reduce函数:

1、

例1

1 num_l = [2,4,10,100]
2 init = 0
3 for i in num_l:
4     init += i
5 print(init)

结果:116

例2

1 num_l = [2,4,10,100]
2 def sum_test(array):
3     init = 0
4     for i in array:
5         init += i
6     return init
7 f = sum_test(num_l)
8 print(f)

结果:116

例3

1 num_l = [2,4,10,100]
2 init = 1
3 for i in num_l:
4     init *= i
5 print(init)

结果8000

例4

1 num_l = [2,4,10,100]
2 def mul(array):
3     init = 1
4     for i in array:
5         init *= i
6     return init
7 
8 f = mul(num_l)
9 print(f)

结果:8000

例5

 1 num_l = [2,4,10,100]
 2 def reduce_test(func,array,init=None):
 3     init = array.pop(0)
 4     for i in array:
 5         init = func(init,i)
 6     return init
 7 
 8 def product(x,y):
 9     return x * y
10 
11 f = reduce_test(product,num_l,)
12 print(f)

结果:8000

例6

 1 num_l = [2,4,10,100]
 2 def reduce_test(func,array,init=None):
 3     if init == None:
 4         res = array.pop(0)
 5     else:
 6         res = init
 7     for i in array:
 8         res = func(res,i)
 9     return res
10 
11 def product(x,y):
12     return x * y
13 
14 f = reduce_test(product,num_l,4)
15 print(f)

结果:32000

例7

 1 ############# reduce 函数
 2 #使用前需要导入reduce函数包
 3 from functools import reduce
 4 
 5 num_l = [2,4,10,100]
 6 def product(x,y):
 7     return x * y
 8 
 9 f1 = reduce(product,num_l,5)
10 print(f1)
11 
12 f1 = reduce(lambda x,y: x + y + 1,num_l,1000)
13 print(f1)
1 40000
2 1120
原文地址:https://www.cnblogs.com/jianguo221/p/8965645.html