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

  

  

相关文章:

  • 2021-07-31
  • 2022-12-23
  • 2022-03-09
  • 2021-09-01
  • 2021-07-22
猜你喜欢
  • 2021-12-16
  • 2021-12-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-17
  • 2018-01-06
相关资源
相似解决方案