简体   繁体   中英

How to sum values in dict one key each

I'm working with API and I got a values(number in float). 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. How to fix it?

I have tried to sum(mydict.values()) and made it with array

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

您在这里所做的只是获取键号i,并将其加起来没有任何内容,然后将其增加到变量i中,但是变量中的值将替换为键号,我需要另一个键值来添加每个值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM