I'm trying to achieve the following:
given
a = [{'val': 10, 'count': 1}]
b = [{'val': 10, 'count': 4}, {'val': 20, 'count': 2}]
I would like to get
output = [{'val': 10, 'count': 5}, {'val': 20, 'count': 2}]
That is, merging 2 lists of dicts according to val: if in 2 dict instances the val is the same, combine by summing the counts, otherwise keep 2 instances.
By the way, performance is an issue, so an elegant and fast solution is preferred.
Thanks!
If you're willing and able to change your data structure to something a little simpler, like this:
a = {10:1}
b = {10:4, 20:2}
Then you can easily use Counter
:
from collections import Counter
c = Counter()
c.update(a)
c.update(b)
print dict(c)
# Result:
# {10: 5, 20: 2}
Based on @Brionius idea you can manipulate you data using Counter and List comprehension :
We will take the data and create a dict that we can use easily with Counter, then we can add up the data and finally revert back to the format we want(your original format).
from collections import Counter
a = [{'val': 10, 'count': 1}]
b = [{'val': 10, 'count': 4}, {'val': 20, 'count': 2}]
c = Counter()
[c.update({d['val']:d['count']}) for d in a + b]
print [{'val': k, 'count': v} for k, v in c.iteritems()]
Output:
[{'count': 5, 'val': 10}, {'count': 2, 'val': 20}]
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.