DDD
DDD

Reputation: 1

Sum the values ​of a list of nested dictionaries that have the same key

I want to sum a list of nested dictionaries with the same key. This is the list:

[{'CAG': {'PG': 4, 'punti': 0, 'Gol fatti': 0, 'Gol subiti': 3, 'Differenza reti': -3, 'Vittorie': 0, 'Pareggi': 0, 'Sconfitte': 1}}, 
 {'ROM': {'PG': 4, 'punti': 3, 'Gol fatti': 3, 'Gol subiti': 0, 'Differenza reti': 3, 'Vittorie': 1, 'Pareggi': 0, 'Sconfitte': 0}}, 
 {'CAG': {'PG': 4, 'punti': 3, 'Gol fatti': 1, 'Gol subiti': 0, 'Differenza reti': 1, 'Vittorie': 1, 'Pareggi': 0, 'Sconfitte': 0}}, 
 {'ROM': {'PG': 4, 'punti': 0, 'Gol fatti': 0, 'Gol subiti': 1, 'Differenza reti': -1, 'Vittorie': 0, 'Pareggi': 0, 'Sconfitte': 1}}, 
 {'CAG': {'PG': 4, 'punti': 0, 'Gol fatti': 0, 'Gol subiti': 1, 'Differenza reti': -1, 'Vittorie': 0, 'Pareggi': 0, 'Sconfitte': 1}}]

I would like to check if they have the same primary key (i.e 'CAG') and then sum the corresponding values.

Final result:

[{'ROM': {'PG': 8, 'punti': 3, 'Gol fatti': 3, 'Gol subiti': 1, 'Differenza reti': 2, 'Vittorie': 1, 'Pareggi': 0, 'Sconfitte': 1}}, 
 {'CAG': {'PG': 12, 'punti': 3, 'Gol fatti': 1, 'Gol subiti': 4, 'Differenza reti': -3, 'Vittorie': 1, 'Pareggi': 0, 'Sconfitte': 2}}]

Upvotes: 0

Views: 44

Answers (1)

Tomerikoo
Tomerikoo

Reputation: 19414

The Counter class supports addition which would make this quite easy. Combining that with a defaultdict of Counters we can get:

from collections import Counter, defaultdict

l = [{'CAG': {'PG': 4, 'punti': 0, 'Gol fatti': 0, 'Gol subiti': 3, 'Differenza reti': -3, 'Vittorie': 0, 'Pareggi': 0, 'Sconfitte': 1}},
 {'ROM': {'PG': 4, 'punti': 3, 'Gol fatti': 3, 'Gol subiti': 0, 'Differenza reti': 3, 'Vittorie': 1, 'Pareggi': 0, 'Sconfitte': 0}},
 {'CAG': {'PG': 4, 'punti': 3, 'Gol fatti': 1, 'Gol subiti': 0, 'Differenza reti': 1, 'Vittorie': 1, 'Pareggi': 0, 'Sconfitte': 0}},
 {'ROM': {'PG': 4, 'punti': 0, 'Gol fatti': 0, 'Gol subiti': 1, 'Differenza reti': -1, 'Vittorie': 0, 'Pareggi': 0, 'Sconfitte': 1}},
 {'CAG': {'PG': 4, 'punti': 0, 'Gol fatti': 0, 'Gol subiti': 1, 'Differenza reti': -1, 'Vittorie': 0, 'Pareggi': 0, 'Sconfitte': 1}}]

counter = defaultdict(Counter)
for d in l:
    for key, value in d.items():
        counter[key] += Counter(value)

print(counter)

Using a defaultdict we create an empty Counter for each potential key ('CAG'/'ROM'). Then for each key we encounter we add up its counter with a Counter from the values.

The output will be:

defaultdict(<class 'collections.Counter'>, {'CAG': Counter({'PG': 12, 'Gol subiti': 4, 'punti': 3, 'Sconfitte': 2, 'Gol fatti': 1, 'Vittorie': 1}), 'ROM': Counter({'PG': 8, 'punti': 3, 'Gol fatti': 3, 'Differenza reti': 2, 'Vittorie': 1, 'Gol subiti': 1, 'Sconfitte': 1})})

And to get a list output a simple manipulation will do:

print([{key: dict(counts)} for key, counts in counter.items()])

Will give:

[{'CAG': {'PG': 12, 'Gol subiti': 4, 'Sconfitte': 2, 'punti': 3, 'Gol fatti': 1, 'Vittorie': 1}}, 
 {'ROM': {'PG': 8, 'punti': 3, 'Gol fatti': 3, 'Differenza reti': 2, 'Vittorie': 1, 'Gol subiti': 1, 'Sconfitte': 1}}]

Upvotes: 1

Related Questions