Dongyu Fu
Dongyu Fu

Reputation: 33

Python: sum up the score1 and score2 fields for all dicts inside the list which have the same teamA and teamB?

I have no ideas with counting the dictionary in a list. just like the list of instances

we have a list like this

list_of_dict = [{'teamA': 'Ocean Gaming ', 'teamB': 'PSISTORM Gaming', 'score1': '0', 'score2': '1'},
    {'teamA': 'Ocean Gaming ', 'teamB': 'PSISTORM Gaming', 'score1': '0', 'score2': '1'},
    {'teamA': 'Ocean Gaming ', 'teamB': 'PSISTORM Gaming', 'score1': '1', 'score2': '0'},
    {'teamA': 'Jin Air Green Wings ', 'teamB': 'Invictus Gaming', 'score1': '1', 'score2': '0'},
    {'teamA': 'Jin Air Green Wings ', 'teamB': 'Invictus Gaming', 'score1': '1', 'score2': '0'},
    {'teamA': 'Jin Air Green Wings ', 'teamB': 'Invictus Gaming', 'score1': '1', 'score2': '0'}]

I expect after counting output like lists under

list_of_dict = [
{'teamA': 'Ocean Gaming ', 'teamB': 'PSISTORM Gaming', 'score1': '1', 'score2': '2'}
{'teamA': 'Jin Air Green Wings ', 'teamB': 'Invictus Gaming', 'score1': '3', 'score2': '0'}
]

Upvotes: 2

Views: 46

Answers (2)

shriakhilc
shriakhilc

Reputation: 3000

@umn 's answer is more elegant and may be more optimized, so prefer that if you can use numpy in your script. Below is a simple way to do so without additional libraries:

list_of_dict = [{'teamA': 'Ocean Gaming ', 'teamB': 'PSISTORM Gaming', 'score1': '0', 'score2': '1'},
    {'teamA': 'Ocean Gaming ', 'teamB': 'PSISTORM Gaming', 'score1': '0', 'score2': '1'},
    {'teamA': 'Ocean Gaming ', 'teamB': 'PSISTORM Gaming', 'score1': '1', 'score2': '0'},
    {'teamA': 'Jin Air Green Wings ', 'teamB': 'Invictus Gaming', 'score1': '1', 'score2': '0'},
    {'teamA': 'Jin Air Green Wings ', 'teamB': 'Invictus Gaming', 'score1': '1', 'score2': '0'},
    {'teamA': 'Jin Air Green Wings ', 'teamB': 'Invictus Gaming', 'score1': '1', 'score2': '0'}]

intermediate_dict = {}

for d in list_of_dict:
    key = (d['teamA'], d['teamB'])
    if key in intermediate_dict.keys():
        intermediate_dict[key][0] += int(d['score1'])
        intermediate_dict[key][1] += int(d['score2'])
    else:
        intermediate_dict[key] = [int(d['score1']), int(d['score2'])]

final_list = []
for k,v in intermediate_dict.items():
    temp_dict = {}
    temp_dict['teamA'] = k[0]
    temp_dict['teamB'] = k[1]
    temp_dict['score1'] = v[0]
    temp_dict['score2'] = v[1]
    final_list.append(temp_dict)

print(final_list)

Upvotes: 0

umn
umn

Reputation: 471

import pandas as pd
import numpy as np

list_of_dict = [{'teamA': 'Ocean Gaming ', 'teamB': 'PSISTORM Gaming', 'score1': '0', 'score2': '1'},
    {'teamA': 'Ocean Gaming ', 'teamB': 'PSISTORM Gaming', 'score1': '0', 'score2': '1'},
    {'teamA': 'Ocean Gaming ', 'teamB': 'PSISTORM Gaming', 'score1': '1', 'score2': '0'},
    {'teamA': 'Jin Air Green Wings ', 'teamB': 'Invictus Gaming', 'score1': '1', 'score2': '0'},
    {'teamA': 'Jin Air Green Wings ', 'teamB': 'Invictus Gaming', 'score1': '1', 'score2': '0'},
    {'teamA': 'Jin Air Green Wings ', 'teamB': 'Invictus Gaming', 'score1': '1', 'score2': '0'}]

df = pd.DataFrame(list_of_dict, dtype=np.int8)
new_df = df.groupby(['teamA','teamB']).sum().reset_index()

new_list_of_dict = new_df.to_dict('records')

print(new_list_of_dict)

Output:

[{'teamA': 'Jin Air Green Wings ', 'teamB': 'Invictus Gaming', 'score1': 3, 'score2': 0}, {'teamA': 'Ocean Gaming ', 'teamB': 'PSISTORM Gaming', 'score1': 1, 'score2': 2}]

Upvotes: 2

Related Questions