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
posted @ 2020-03-23 18:31  -费费  阅读(282)  评论(0编辑  收藏  举报