Official explanation:
Apply function of arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to A single value. For example, reduce (lambda x, Y:x+y, [1, 2, 3, 4, 5]) calculates (((((1+2) +3) +4) +5). The left argument, X, are the accumulated value and the right argument, y, are the update value from the iterable. If the optional initializer is present, it's placed before the items of the iterable in the calculation, and serves as a Default when the iterable is empty. If initializer is not given and Iterable contains only one item, the first item is returned. Roughly equivalent to:
This means: To apply an iterative object to a method with two parameters, we call it appfun, iterate over the iterative object, and the elements in turn as Appfun parameters, but this function has two parameters, which parameter? With this rule, take a look at the following implementation of the reduce method, there are three parameters, the first parameter is the above said Appfun, the second parameter is the object that can be iterated, and the third one? When the reduce method is called when the initializer parameter is given, then the first call to appfun this parameter value as the first parameter, and the elements of the object can be iterated as the second parameter of the Appfun If the initializer parameter is not given when the call to reduce is called, then the first element of the object can be iterated as the first element of the Appfun, and the second parameter of the iterator from the second element to the last, as Appfun, is the first time the appfun is called. In addition to the first call, the first parameter of Appfun is the return value of Appfun. For example, reduce (lambda x, Y:x+y, [1, 2, 3, 4, 5]), calculates the and of 1 to 5, because there is no given initializer parameter, so the first call to X+y, X=1, that is, the first element of the list, y=2, the second element of the list, and then return 1 +2 results as the second call to X in X+y, that is, the last result, y=2, the second element, and so on, to know the result of getting the 1+2+3+4+5.
So, in fact, the following code definition is a bit of a problem, we call this code in the program reduce (lambda x, Y:x+y, [1, 2, 3, 4, 5]), the result is 16, and the correct result is 15, the problem is that if the collection does not start with 0, then according to the following code, The first call to X=1, that is, the first element, Y is also equal to 1, and the first element, and the correct y should be 2. So the real reduce method should be different from the following example.
def reduce (function, iterable, initializer=none): it = iter (iterable) if initializer is None: try: initializer = next (it) except stopiteration: raise TypeError (' reduce () of empty sequence with no initial value ') C6/>accum_value = initializer for x in iterable: accum_value = function (Accum_value, x)
So what can the reduce function do, and in what case use reduce, look at the following example:
For example, the above example implements an accumulation of a shaping set. Assuming LST = [1,2,3,4,5], there are a number of ways to achieve accumulation:
The first type: Use the SUM function .
The second type: loop mode.
def customer_sum (LST): result = 0 for x in LST: result+=x return result #或者 def customer_sum (LST): C5/>result = 0 while lst: temp = lst.pop (0) result+=temp return result if __name__== "__main__": C11/>lst = [1,2,3,4,5]
The Third Kind: recursive summation
def add (lst,result): if lst: temp = lst.pop (0) Temp+=result return Add (lst,temp) else: return result if __name__== "__main__": lst = [1,2,3,4,5]
Fourth type: Reduce mode
LST = [1,2,3,4,5] print reduce (lambda x,y:x+y,lst) #这种方式用lambda表示当做参数 because the third parameter of reduce is not provided, so the first execution is x=1,y=2, the second time x=1+2,y= 3, the third element of the list #或者 lst = [1,2,3,4,5] print reduce (lambda x,y:x+y,lst,0) #这种方式用lambda表示当做参数 because the third parameter with reduce is specified as 0, So for the first time X=0,y=1, the second x=0+1,y=2, the second element of the list, assuming that the third parameter of reduce is 100, the first execution x=100,y is still the element that iterates through the list, and the result is a #或者 def add (x, y): return x+y print reduce (add, LST) #与方式1相同, just replace lambda expression with custom function #或者 def add (x, y): return X+y
One more example: There is a sequence set, such as [1,1,2,3,2,3,3,5,6,7,7,6,5,5,5], which counts the number of repetitions of all the keys in this set, such as 1 appearing two times, 2 appearing two times, and so on. The general idea is to use a dictionary store, the element is the dictionary key, the number of occurrences is the value of the dictionary. Still a lot of methods
The first type: For loop judgment
def statistics (LST): dic = {} for K in LST: if not k in dic: dic[k] = 1 else: dic[k] +=1 return dic
The second kind: compare trickery, first put the list in set mode, and then use the list of the Count method
def STATISTICS2 (LST): m = set (LST) dic = {} for x in M: dic[x] = lst.count (x) return dic
The Third kind: using the Reduce method
def statistics (DIC,K): if not k in dic: dic[k] = 1 else: dic[k] +=1 return dic lst = [1,1,2,3 , 2,3,3,5,6,7,7,6,5,5,5] print reduce (statistics,lst,{}) #提供第三个参数, for the first time, the initial dictionary is empty, as the first parameter of the statistics, and then the LST is traversed, As the second argument, and then returns the dictionary collection as the first parameter of the next
In the above example, it is found that in general, the reduction method can be used to solve a problem that can be solved by a loop or a recursive method if a set is to be operated on and has a statistical result.
The reduce function is really "a good comrade"!