gpt4 book ai didi

python - 对不同列表中具有相同索引的所有元素求和

转载 作者:行者123 更新时间:2023-11-28 16:29:02 24 4
gpt4 key购买 nike

score=[
['5.00', '4.30', '2.50', '3.60', '1.30', '5.00'],
['80.00', '8.00', '32.00', '13.60', '20.00', '80.00'],
['12.00', '6.00', '7.20', '8.40', '9.60', '12.00'],
['3.00', '1.20', '2.04', '1.08', '0.84', '3.00']
]

我要

a = 5 + 80 + 12 + 3   # add first element of all lists in score
b = 4.3 + 8 + 6 + 1.2 # add second element of all lists in score
c = 2.5 + 32 + 7.2 + 2.04 # etc ...
d = 3.6 + 13.6 + 8.4 + 1.08 # ...
e = 1.3 + 20 + 9.6 + 0.84
f = 5 + 80 + 12 + 3

但是我不知道score中有多少个列表,所以我不能使用zip()。如何在 python 的不同列表中对具有相同索引的所有元素求和?

最佳答案

实际上,你可以使用zip :

>>> map(sum, map(lambda l: map(float, l), zip(*score)))
[100.0, 19.5, 43.74, 26.68, 31.74, 100.0]

关于python - 对不同列表中具有相同索引的所有元素求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33981330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com