[英]How to sum values in dict one key each
I'm working with API and I got a values(number in float). 我正在使用API,但有一个值(浮点数)。 I wrote down in dict and after that i should to sum values.
我在字典中写下来,然后我应该对值求和。 But after summed it up I have the last value which was write down in dict.
但是总结起来之后,我得到了在dict中写下的最后一个值。 How to fix it?
如何解决?
I have tried to sum(mydict.values()) and made it with array 我试图求和(mydict.values())并使其与数组
sums={'suma': [274.6243,
140.6846,
1168.3088,
657.7995,
7.7,
591.7394,
543.3994,
121.836,
259624.1855,
1200.8789,
271.6997,
271.6997,
545.0,
3.15,
22.24,
308.4797,
3.15,
2199.6225,
4375.3625,
7.7,
43.62,
7.7,
543.3994,
81.26,
279.6997,
87.516,
0.0,
4295.9209,
0.5688,
126.984,
286.2397,
2475.852,
3.1988,
584.74,
183.2181,
716.0388,
22.0,
1086.7989,
7.7,
212.11,
1262940816.0582,
279.3997,
165.6996,
271.6997,
4941.7034,
2024.3652,
129.1241,
44.0,
9.45,
271.6997]}
for i in range(len(sums)):
suma=sum(sums[i].values())
print(suma)
I expect the output of 1263232337.0252
, but the actual output is 271.6997
我期望输出
1263232337.0252
,但实际输出是271.6997
您在这里所做的只是获取键号i,并将其加起来没有任何内容,然后将其增加到变量i中,但是变量中的值将替换为键号,我需要另一个键值来添加每个值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.