filter functionmap functionreduce function introduction

filter traverses each element in the sequence, determines the boolean value obtained by each element, and if it is True, leaves the 

primary version
 1 movie_people = ['lining','zhaoheng','m aoxin','m_lining','m_zhaoheng']
 2 def m_show(n):
 3     return n.endswith('m')
 4 #lambda n: n.endswith('m')
 5 
 6 def filter_test(fun,array):
 7     ret = []
 8     for p in array:
 9         if not fun(p):
10             ret.append(p)
11     return ret
12 
13 
14 # res = filter_test(m_show,movie_people)
15 # print(res)
16 
17 res = filter_test(lambda n: n.endswith('m'),movie_people)
18 print(res)

Premium version 

1 #filter 函数
2 movie_people = ['lining', 'zhaoheng', 'm aoxin', 'm_lining', 'm_zhaoheng']
3 res = filter(lambda n: not  n.endswith('m'),movie_people)
4 print(list(res))
# map is a built-in function, and its two parameters, fun, mainly implement simple functions for iterable objects. array is an iterable object.

Primary version 

1 num_1 = [1,2,3,4,5]    
2 def map_test(array):   
3     ret = []           
4     for i in array:    
5         ret.append(i+1)
6     return ret         
7 print(map_test(num_1)) 

Primary Version 2 

 1 num_1 = [1,2,3,4,5]                 
 2 def add_one(x):                     
 3     return x+1                      
 4 def reduce_one(x):                  
 5     return x-1                      
 6 
 7 def map_test(fun,array):            
 8     ret = []                        
 9     for i in array:                 
10         res = fun(i)                
11         ret.append(res)             
12     return ret                      
13 print(map_test(add_one,num_1))      
14 print(map_test(lambda x:x+1,num_1)) 

Primary Version 3

 1 num_1 = [1,2,3,4,5]                    
 2 def map_test(fun,array):               
 3     ret = []                           
 4     for i in array:                    
 5         res = fun(i)                   
 6         ret.append(res)                
 7     return ret                         
 8 print(map_test(lambda x:x+1,num_1))    
 9 print(map_test(lambda x:x-1,num_1))    
10 print(map_test(lambda x:x**2,num_1))   
11                                        

Premium version 

 1 res = map(lambda x:x+1,num_1)

 2 print(list(res))  

#reduce : process a sequence, and then merge the sequence into operation 
primary version 1
 
1 num_1 = [1,2,3,4,12 ]
 2 res = 0
 3  for num in num_1:
 4      res+= num
 5  print (res)
 

Primary Version 2

 1 def reduce_test(func,array,init=100):
 2     if init is None:
 3         res = array.pop(0)
 4     else:
 5         res = init
 6     for num in array:
 7         res=func(res,num)
 8     return res
 9 
10 ret = reduce_test(lambda x,y:x*y,num_1)
11 print(ret)

Premium version 

1 from functools import reduce
2 num_1 = [1,2,3,4,12]
3 res = reduce(lambda x,y:x*y,num_1,100)
4 print(res)

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324820991&siteId=291194637