Home  >  Article  >  Backend Development  >  Example of reduce() function in python

Example of reduce() function in python

黄舟
黄舟Original
2017-10-03 05:44:491646browse

The reduce() function will accumulate elements in the parameter sequence. The following article mainly introduces you to the use of the reduce() function in python. The article introduces it in great detail through example code. It is very useful for everyone's learning or The work has certain reference and learning value. Friends who need it can take a look below.

Preface

This article mainly introduces the relevant content about the use of reduce() function in python, and shares it for your reference and study. Not much to say below, let’s take a look at the detailed introduction: The

reduce() function is in the library functools. If you want to use it, you must import it from this library. The reduce function is different from the map function. The map operation is a parallel operation. The reduce function is an operation that combines multiple parameters, that is, the result of simplification from multiple conditions. In computer algorithms, in most cases, it is to simplify. For example, to identify whether an image is a cat is to extract a judgment from many pixels: yes or no. There may be millions of pixels, but only one result will come out. In GOOGLE's large-scale cluster, this idea is used to call the previous parallel processing operations map. The results after parallel processing need to be simplified and classified. This process of simplification and classification is called reduce. Since reduce can only operate on one host and cannot be processed in a distributed manner, reduce processes map results, which means that these results are very simple, the amount of data is greatly reduced, and the processing is very fast.

Therefore, the mapreduce process can be called the process of analysis and induction.

Look at the example of reduce():


#python 3. 6 
#蔡军生 
#http://blog.csdn.net/caimouse/article/details/51749579 
# 
from functools import reduce 
 
result = reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) 
print(result)

Output Result:

15

In this example, the calculation process is actually like this:

((((1 +2)+3)+4)+5)


##Look at another factorial example:


#python 3. 6 
#蔡军生 
#http://blog.csdn.net/caimouse/article/details/51749579 
# 
from functools import reduce 
 
n = 3 
print(reduce(lambda x, y: x * y, range(1, n + 1))) # 6

Output result:

6

reduce function, the reduce function will elements are accumulated.


The definition of reduce function:


reduce(function, sequence[, initial]) -> value

The function parameter is a function with two parameters, reduce takes an element from the sequence in turn, and calls the function again with the result of the last call to the function as a parameter.


When the function is called for the first time, if the initial parameter is provided, the function will be called with the first element in the sequence and initial as parameters, otherwise the first two elements in the sequence will be used as parameters. Call function.



reduce(lambda x, y: x + y, [2, 3, 4, 5, 6], 1)

The result is 21( (((((1+2)+3)+4)+5)+6) )



reduce(lambda x, y: x + y, [2, 3, 4, 5, 6])

The result is 20

Summary

The above is the detailed content of Example of reduce() function in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn