小编Bor*_*s P的帖子

python中map()函数的多个迭代器?

我知道有一种方法可以在一个命令中为多个迭代器执行 python map 函数,但我不断收到语法或 valueErrors,或者如果它确实编译它只是覆盖以前的迭代或给出错误的答案。我究竟做错了什么?

num = ["1","2","3"]
num2 =["4","5","6"]
num3 = ["7","8","9"]

j = list(map(lambda x, y, z: int(x) and int(y) and int(z), num, num2, num3))
print(j)   #[7, 8, 9]
print(reduce(lambda x, y: x+y, j))   #24
Run Code Online (Sandbox Code Playgroud)

目标是将多个列表中的所有数字从字符串映射到整数到一个大列表中,然后使用 reduce() 求和

j 应该出来 [1,2,3,4,5,6,7,8,9] 所有整数

减少应将其总和为 45

更新:这种方式也不起作用

num = ["1","2","3"]
num2 =["4","5","6"]
num3 = ["7","8","9"]

j = list(map(lambda x, y, z: int(x+y+z), num, num2, num3))
print(j)  #[147, 258, 369]
print(reduce(lambda x, y: x+y, j)) #774
Run Code Online (Sandbox Code Playgroud)

python iterator

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

标签 统计

iterator ×1

python ×1