Python: reduce () function

description

reduce () function parameter sequence elements have accumulated.

All data is a function of data set (a list, tuple, etc.) are the following: (two parameters) of the first set of first and second elements with the function operates in the function passed to reduce, obtained results further data to the third function operation function, to obtain a final result.
grammar

reduce () function syntax:

reduce(function, iterable[, initializer])

parameter

function -- 函数,有两个参数
iterable -- 可迭代对象
initializer -- 可选,初始参数

return value

Function returns the results.
Examples

The following example shows reduce () method is used:

>>> def add (x, y): # adding two numbers

… return x + y

>>> reduce (add, [1,2,3,4,5]) # calculates list: 1 + 2 + 3 + 4 + 5

15

>>> reduce (lambda x, y: x + y, [1,2,3,4,5]) # anonymous function using lambda

15

Guess you like

Origin blog.csdn.net/weixin_44523387/article/details/93371523