python reduce() 函数
reduce() 函数会对参数序列中元素进行累积。
from functools import reduce
def add(x, y) : # 两数相加
return x + y
reduce(add, [1,2,3,4,5]) # 计算列表和:1+2+3+4+5
# 15
reduce(lambda x, y: x+y, [1,2,3,4,5]) # 使用 lambda 匿名函数
# 15
reduce() 函数会对参数序列中元素进行累积。
from functools import reduce
def add(x, y) : # 两数相加
return x + y
reduce(add, [1,2,3,4,5]) # 计算列表和:1+2+3+4+5
# 15
reduce(lambda x, y: x+y, [1,2,3,4,5]) # 使用 lambda 匿名函数
# 15