Issue
My question is similar to this(Python sum on keys for List of Dictionaries), but need to sum up the values based on two or more key-value elements. I have a list of dictionaries as following:
list_to_sum=
[{'Name': 'A', 'City': 'W','amt':100},
{'Name': 'B', 'City': 'A','amt':200},
{'Name': 'A', 'City': 'W','amt':300},
{'Name': 'C', 'City': 'X','amt':400},
{'Name': 'C', 'City': 'X','amt':500},
{'Name': 'A', 'City': 'W','amt':600}]
So based on a combination of Name and City key values, amt should be summed. Please let me know how to solve this.
Output: [{'Name': 'A', 'City': 'W','amt':900},
{'Name': 'B', 'City': 'A','amt':200},
{'Name': 'C', 'City': 'X','amt':900}]
Solution
You could create a collections.Counter
.Then you can simply add the values as the appear using the tuple as (Name, City)
as the key:
from collections import Counter
list_to_sum=[
{'Name': 'A', 'City': 'W','amt':100},
{'Name': 'B', 'City': 'A','amt':200},
{'Name': 'A', 'City': 'W','amt':300},
{'Name': 'C', 'City': 'X','amt':400},
{'Name': 'C', 'City': 'X','amt':500},
{'Name': 'A', 'City': 'W','amt':600}
]
totals = Counter()
for d in list_to_sum:
totals[(d['Name'],d['City'])] += d['amt']
print(totals[('A','W')]) # 1000
print(totals[('B','A')]) # 200
print(totals[('C','X')]) # 900
This will produce a dictionary-like object Counter
:
Counter({('A', 'W'): 1000, ('B', 'A'): 200, ('C', 'X'): 900})
With this you can convert the dict back to a list of dicts like:
sums_list = [{'Name':Name, 'City':City, 'amt':amt} for (Name, City), amt in totals.items()]
giving sums_list
:
[{'Name': 'A', 'City': 'W', 'amt': 1000},
{'Name': 'B', 'City': 'A', 'amt': 200},
{'Name': 'C', 'City': 'X', 'amt': 900}]
Answered By - Mark
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.