Home  >  Article  >  Backend Development  >  What is reduce in python

What is reduce in python

silencement
silencementOriginal
2019-05-21 15:27:097580browse

What is reduce in python

reduce() The function accumulates elements in the parameter sequence.

The function performs the following operations on all data in a data set (linked list, tuple, etc.): Use the function function (with two parameters) passed to reduce to first , operate on 2 elements, and the result obtained is then operated with the third data using the function function, and finally a result is obtained.

reduce() function receives three parameters, as shown below

What is reduce in python

reduce() function syntax:

reduce(function, iterable[, initializer])

The working process of reduce is: in the process of iterating sequence (tuple, list, dictionary, string and other iterable objects), first pass the first two elements to the function parameters. After the function processes, then The obtained result and the third element are passed as two parameters to the function parameter, and the result obtained after function processing and the fourth element are passed as two parameters to the function parameter, and so on. If the initial value is passed in, then the first and second elements of the sequence are not passed first, but the initial value and the first element. After such cumulative calculations, the sequence is merged into a single return value

For example

What is reduce in python

Look at another example

What is reduce in python

Related learning recommendations: python tutorial

The above is the detailed content of What is reduce 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