reduce() 函数

reduce() 函数会对参数序列中元素进行累加

用法:

reduce(function, iterable[, initializer])

  • function -- 函数,两个参数
  • iterable -- 可迭代对象(链表,元组等)
  • initializer -- 可选,初始参数

实例

from functools import reduce
def add(x, y):
    return x+y
s = reduce(add, [1, 2, 3, 4, 5])  # ((((1+2)+3)+4)+5)
print(s)  # 15
原文地址:https://www.cnblogs.com/alivinfer/p/12554061.html