如何获取将python字典的总和列为元组?
我的python词典被列为如下所示;在这里,每个元组的第一个值描绘了组号,每个字典描述了与组内部的每个数字相关的生成值(ie,{'1':0,'3':1,'2':1, ,'303':3}
[('1',{'1': 0,'3': 1,'2': 1,'5': 2,'303': 3}), ('2',{'4': 0,'5': 1,'7': 1,'5': 2,'303': 2}), ('3',{'1': 0,'3': 0,'2': 0,'5': 2,'303': 3})]
是否有任何方法来获取每个组的总和
[('1', 7), ('2', 6), ('3', 5)]
'5': 2 这种方法是如此昂贵。
I have python dictionary listed as tuples as follows; Here the first value of the each tuple depicts the group number and each dictionary depicts the generated value related to the each number inside the group, (i.e., {'1': 0,'3': 1,'2': 1,'5': 2,'303': 3}.
[('1',{'1': 0,'3': 1,'2': 1,'5': 2,'303': 3}), ('2',{'4': 0,'5': 1,'7': 1,'5': 2,'303': 2}), ('3',{'1': 0,'3': 0,'2': 0,'5': 2,'303': 3})]
Is there any method to get the sum of the each group by getting the sum of the each group, (i.e.,
[('1', 7), ('2', 6), ('3', 5)]
I tried to iterate over the each value and get the sum but that method is so expensive.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个简单的列表理解应该如此
A simple list comprehension should so