Distinguish the difference between map and reduce functions function map function and reduce functions

In terms of parameters from ①: 
Map () function:
  Map () contains two parameters, the first parameter is a function of the second sequence is (as lists or tuples). Wherein the function (i.e., the first argument map of the location) may receive one or more parameters.
reduce () function:
The first parameter reduce () is a function of the second sequence is (as lists or tuples). However, the function must receive two parameters.

② From the values pass in terms of effect:
Map () function is passed sequentially applied to each element of the sequence, each element is a function of its own "action" once;
the reduce () is a function of the successor after the effect of the first element of a sequence obtained results, the results of this action continues with the next element (cumulative basis),
the final result is a result of the interaction of all the elements.
Exercise:
Calculated: lambda implement and reduce the accumulation 1-100
 from functools Import reduce
 Print (reduce ( the lambda X, Y: SUM ([X, Y]), Range (1,101)))   # The first parameter is a function of the second parameter is a sequence 
Print (Map ( the lambda X, Y: SUM ([X, Y]), Range (1,101)))   # The first parameter is a function of the second parameter is a sequence 
Print (List (Map ( the lambda X, Y: SUM ([X, Y]), Range (1,101), Range (1,101))))   # The first parameter is a function of the second parameter is a sequence

Print Results:

 

In terms of parameters from ①: 
Map () function:
  Map () contains two parameters, the first parameter is a function of the second sequence is (as lists or tuples). Wherein the function (i.e., the first argument map of the location) may receive one or more parameters.
reduce () function:
The first parameter reduce () is a function of the second sequence is (as lists or tuples). However, the function must receive two parameters.

② From the values pass in terms of effect:
Map () function is passed sequentially applied to each element of the sequence, each element is a function of its own "action" once;
the reduce () is a function of the successor after the effect of the first element of a sequence obtained results, the results of this action continues with the next element (cumulative basis),
the final result is a result of the interaction of all the elements.
Exercise:
Calculated: lambda implement and reduce the accumulation 1-100
 from functools Import reduce
 Print (reduce ( the lambda X, Y: SUM ([X, Y]), Range (1,101)))   # The first parameter is a function of the second parameter is a sequence 
Print (Map ( the lambda X, Y: SUM ([X, Y]), Range (1,101)))   # The first parameter is a function of the second parameter is a sequence 
Print (List (Map ( the lambda X, Y: SUM ([X, Y]), Range (1,101), Range (1,101))))   # The first parameter is a function of the second parameter is a sequence

Print Results:

 

Guess you like

Origin www.cnblogs.com/maaosheng/p/11618788.html