[英]How to sum dictionaries values with same key inside a list?
I have this list: 我有这个清单:
list1 = [
{'currency': 'USD', 'value': 10},
{'currency': 'USD', 'value': 12},
{'currency': 'EUR', 'value': 11},
{'currency': 'EUR', 'value': 15},
{'currency': 'EUR', 'value': 17},
{'currency': 'GBP', 'value': 13},
]
How do I combine the dictionaries so I get this list from list1? 如何组合字典,以便从list1获取此列表?
list2 = [
{'currency': 'USD', 'value': 22},
{'currency': 'EUR', 'value': 43},
{'currency': 'GBP', 'value': 13},
]
Use a dictionary: 使用字典:
d = {}
for x in list1:
c, v = x["currency"], x["value"]
d[c] = d.get(c, 0) + v
# {'EUR': 43, 'GBP': 13, 'USD': 22}
Then either use that dict directly (I would recommend that) or turn it back into your list-of-dicts format, use a list-comprehension: 然后直接使用该dict(我建议)或将其转回你的list-of-dicts格式,使用list-comprehension:
>>> [{"currency": k, "value": v} for k, v in d.items()]
[{'currency': 'USD', 'value': 22},
{'currency': 'EUR', 'value': 43},
{'currency': 'GBP', 'value': 13}]
Using collections.defaultdict
. 使用
collections.defaultdict
。
Demo: 演示:
import collections
d = collections.defaultdict(int)
list1 = [
{'currency': 'USD', 'value': 10},
{'currency': 'USD', 'value': 12},
{'currency': 'EUR', 'value': 11},
{'currency': 'EUR', 'value': 15},
{'currency': 'EUR', 'value': 17},
{'currency': 'GBP', 'value': 13},
]
for i in list1:
d[i['currency']] += i["value"]
print( [{'currency': k, 'value': v} for k,v in d.items()] )
Output: 输出:
[{'currency': 'USD', 'value': 22}, {'currency': 'GBP', 'value': 13}, {'currency': 'EUR', 'value': 43}]
you can use Counter
to calculate the summs 你可以用
Counter
来计算总和
from collections import Counter
c = Counter()
for d in list1:
cur, v = d.get('currency'), d.get('value')
c.update({cur: v})
print(c)
Counter({'EUR': 43, 'USD': 22, 'GBP': 13})
and after generate the output: 并在生成输出后:
list2 = [{'currency': cur, 'value': v} for cur, v in c.items()]
print(list2)
[{'currency': 'USD', 'value': 22}, {'currency': 'GBP', 'value': 13}, {'currency': 'EUR', 'value': 43}]
This should do it: 这应该这样做:
list1 = [
{'currency': 'USD', 'value': 10},
{'currency': 'USD', 'value': 12},
{'currency': 'EUR', 'value': 11},
{'currency': 'EUR', 'value': 15},
{'currency': 'EUR', 'value': 17},
{'currency': 'GBP', 'value': 13},
]
valuePairs = {}
for d in list1:
curr = d['currency']
val = d['value']
if curr in valuePairs:
valuePairs[curr] += val
else:
valuePairs[curr] = val
solution = [{'currency': k, 'value': v} for k, v in valuePairs.items()]
you could also do: 你也可以这样做:
import itertools as it
list1 = [
{'currency': 'USD', 'value': 10},
{'currency': 'USD', 'value': 12},
{'currency': 'EUR', 'value': 11},
{'currency': 'EUR', 'value': 15},
{'currency': 'EUR', 'value': 17},
{'currency': 'GBP', 'value': 13},
]
kfunc = lambda x: x['currency']
groups = it.groupby(sorted(list1, key=kfunc), kfunc)
result = [{'currency':k, 'value':sum(x['value'] for x in g)} for k, g in groups]
print(result)
You can also one-line it: 你也可以单行:
[{"currency": k, "value": sum([d["value"] for d in list1 if d["currency"] == k])} for k in list(set([d["currency"] for d in list1]))]
list(set([d["currency"] for d in list1]))
gives you the unique list of currencies list(set([d["currency"] for d in list1]))
为您提供唯一的货币列表
You sum then values of each unique currency to make the new dict. 然后将每个唯一货币的值相加,以生成新的dict。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.