A guide to using reduce built in functions in python

  • 2020-04-02 14:02:38
  • OfStack

Official explanation:


Apply function of two 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, is the accumulated value and the right argument, y, is the update value from the iterable. If the optional initializer is present, it is 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 : 

To apply an iterable object to a method with two parameters, we call it appFun, iterate over the iterable object, and take the elements in turn as the parameters of appFun, but this function has two parameters, which parameter? There is a rule, if you look at the implementation of the reduce method, there are three parameters, the first parameter is appFun, the second parameter is the iterable object, and the third parameter? When the reduce method is called, the initializer is given as the parameter. When appFun is called for the first time, the parameter value is taken as the first parameter, while the elements of the iterable object are taken as the second parameter of appFun. If not given, initializer when calls to reduce the parameters, so the first call appFun, the first element of the iterative objects just as the first element of appFun, and can be an iterator from the second element in the end, in turn, as the second parameter of appFun, in addition to the first call, the first parameter to the appFun is appFun return values. Such as reduce (lambda x, y, x + y, [1, 2, 3, 4, 5]), calculated from 1 to 5 and, because there is no given, initializer parameters, so the first call to x + y, x = 1, namely, the first element of the list, y = 2, the list of the second element, then return the result of 1 + 2 as x, in the second call x + y is the result of the last time, y = 2, the second element and so on, know get 1 + 2 + 3 + 4 + 5.

So, in fact, the following code definition is a bit of a problem, we call this code in your application the reduce (lambda x, y, x + y, [1, 2, 3, 4, 5]), the result is 16, and the correct results of 15, the problem is that if the collection is not zero, then according to the following code, the first call to x = 1, which is the first element, y is equal to 1, and the first element, and the right should be 2 y. 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') 
  accum_value = initializer 
  for x in iterable: 
    accum_value = function(accum_value, x) 
  return accum_value 

So what can the reduce function do, and under what circumstances should I use reduce? Look at the following example:

For example, in the above example, the sum of a set of integers is realized. Assuming LST = [1,2,3,4,5], there are many ways to realize the summation:

The first is the sum function .


sum(lst) 

 
The second way: circulation.


def customer_sum(lst): 
  result = 0 
  for x in lst: 
    result+=x 
  return result 
 
# or  
def customer_sum(lst): 
  result = 0 
  while lst: 
      temp = lst.pop(0) 
      result+=temp 
  return result 
 
if __name__=="__main__": 
  lst = [1,2,3,4,5] 
  print customer_sum(lst) 

The third: recursive sums


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] 
  print add(lst,0) 

The fourth way: reduce


lst = [1,2,3,4,5] 
print reduce(lambda x,y:x+y,lst) 
# In this way lambda Represents as a parameter because it is not provided reduce The third parameter of, so when first executed x=1,y=2, The second time x=1+2,y=3, The third element in the list  
 
 
# or  
lst = [1,2,3,4,5] 
print reduce(lambda x,y:x+y,lst,0) 
# In this way lambda Represents as an argument because specified reduce The third parameter of 0 So the first time you execute it x=0,y=1, The second time x=0+1,y=2, The second element of the list,  
 Assume that the specified reduce The third parameter of 100 So the first time x=100,y It's still going through the elements of the list, and the result is zero 115 
 
 
 
# or  
def add(x,y): 
  return x+y 
 
print reduce(add, lst) 
# With the way 1 Same thing, just take lambda The expression was replaced with a custom function  
 
# or  
def add(x,y): 
  return x+y 
 
print reduce(add, lst,0) 
# With the way 2 Same thing, just take lambda The expression was replaced with a custom function  

 
For another example: there is a set sequence,,1,2,3,2,3,3,5,6,7,7,6,5,5,5 [1], for example, the statistical number of repeat this collection all keys 1 appeared twice, for example, 2 appeared twice, etc. The general idea is to store it in a dictionary, where the element is the key of the dictionary, and the number of occurrences is the value of the dictionary. There are still many ways

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 
 
lst = [1,1,2,3,2,3,3,5,6,7,7,6,5,5,5] 
print(statistics(lst)) 

The second way is to use the set method to make the list heavier, and then the count method of the list


def statistics2(lst): 
  m = set(lst) 
  dic = {} 
  for x in m: 
    dic[x] = lst.count(x) 
 
  return dic 
 
lst = [1,1,2,3,2,3,3,5,6,7,7,6,5,5,5] 
print statistics2(lst) 

The third way: use reduce


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,{})  
# The third parameter is provided. The first time, the initial dictionary is empty, as statistics The first parameter of, and then the traversal lst, As the second argument, and then take the returned collection of dictionaries as the first argument next time  
 
 or  
d = {} 
d.extend(lst) 
print reduce(statistics,d) 
# Does not provide a third parameter, but ensures that the first element of the collection is a dictionary object, as statistics The first parameter of, and the traversal collection in turn as the second parameter  

Through the above example, it is found that any problem that can be solved by loop or recursion to operate on a set and have a statistical result can be generally realized by reduce.

Reduce function is really "a good comrade"!


Related articles: