map, reduce and filter (Functional Programming)

# Map may be used to perform the same operation on each element of the structure can be traversed, bulk operations: 
Map (the lambda X: X ** 2, [. 1, 2,. 3,. 4]) # [. 1,. 4,. 9, 16 ] 
Map (the lambda X, Y: X + Y, [. 1, 2,. 3], [. 5,. 6,. 7]) # [. 6,. 8, 10] 
  
# in Python3 species outputs the result 
result1 = list (map (lambda X: X ** 2, [. 1, 2,. 3,. 4])) # [. 1,. 4,. 9, 16] 
Print (RESULT1) 
result2 (Map (the lambda X, Y: X + Y, [. 1, 2, . 3], [. 5,. 6,. 7])) # [. 6,. 8, 10] 
Print (result2) 

# is the reduce the elements of the structure can be traversed two input parameters in the order 
# and stores the result of each as the next first input parameter of the operation, has not yet traversed the elements as the second input parameter 
# the result is that the value of the string can traverse the reduced (the reduce) as an object 
from the reduce functools Import 
RES = the reduce ( X the lambda, Y: X + Y, [. 1, 2,. 3,. 4]) # ((. 1 + 2) +3) = 10. 4 + 
Print (RES)
 
# filter name suggests, can be traversed on the screening structure according to the conditions
filter (lambda x: x% 2, [1, 2, 3, 4, 5]) # odd filter, [1, 3, 5]

  

Guess you like

Origin www.cnblogs.com/lcxiao/p/11361001.html