小编Kum*_*ran的帖子

Python reduce 获取列表中的项目列表

当我研究reduce()Python 中的函数如何工作时,我遇到了这段代码。它打印7,这是预期的输出,并返回 中的项目总数list1,与 的相同len(list1)。我有兴趣知道这段代码是如何工作的。

  1. 0在reduce中做了什么?
  2. 这是否意味着x = list1y = 0?既然如此,为什么返回列表中x + y所有值的总和 ( ) 呢?28
from functools import reduce 
list1 = [1 ,2, 3, 4, 5, 6, 7] 
count  = reduce(lambda x, y : x + 1, list1, 0)
print(count) 
Run Code Online (Sandbox Code Playgroud)

输出:7

python reduce list count

2
推荐指数
1
解决办法
5930
查看次数

标签 统计

count ×1

list ×1

python ×1

reduce ×1