① in terms of parameters:
Map () Function:
Map () contains two parameters, the first one is a function, the second is a sequence (list or tuple). where a function (that is, a function of the first parameter position of a map) can receive one or more parameters .
Reduce () function:
The first parameter of reduce () is a function, and the second is a sequence (list or tuple). However, its function must receive two parameters .
② from the numerical effect on the transfer in terms of:
Map () is the function of passing the incoming functions to each element of the sequence sequentially, each of which is "acting" on its own by the function; (see chestnuts below)
Reduce () is the function of the descendant to the first element of the sequence to get the result, the result will continue to work with the next element (cumulative calculation),
The end result is the interaction of all elements. (See chestnuts below)
Give me a chestnut:
Map () Function:
[Python]View plain copy
- # Pass in a parameter
- def one_p (x):
- return x * x
- Print ' map1.1: ', Map (one_p, Range (1, 5))
- #结果: map1.1: [1, 4, 9, 16]
- Print ' map1.2: ', Map (one_p, [1, 2, 3, 4, 5, 6])
- #结果: map1.2: [1, 4, 9, 16, 25, 36]
- # Incoming multiple parameters
- A = [1, 2, 3, 4, 5]
- b = [1, 1, 6, 2, 3]
- c = [1, 2, 3, 4, 5]
- s = map (lambda (x, Y, z): x * y * z, zip (A, B, c))
- Print ' map2: ', S
- #结果: map2: [1, 4, 54, 32, 75]
Reduce () function:
[Python]View plain copy
- R1 = reduce (lambda x, y:x * y, (2, 2, 6, 2)) #运算过程: (((2*2) *6)
- r2 = reduce (lambda x, y:x * y, (2, 2, 6), 2) #<span > Operation process: ((2*2) *6) </span>
- Print ' R1: ', R1 # Result: r1:48
- Print ' R2: ', R2 # Result: r2:48
Map and reduce functions in Python