Context: I have a dictionary with some soccer results and I'd like to create a second dictionary displaying the values as a list with the soccer match results.
For example, I have this initial dictionary:
dict_results = {('FCP', 'SLB'): (1, 4),
('FCP', 'SCP'): (2, 5),
('SLB', 'FCP'): (2, 6),
('SLB', 'SCP'): (1, 5),
('SCP', 'FCP'): (2, 4),
('SCP', 'SLB'): (1, 4)}
and I'd like to get this format:
tabela = {'FCP': [win, draw, loss, scored_goals, suff_goals, points],
'SLB': [win, draw, loss, scored_goals, suff_goals, points],
'SCP': [win, draw, loss, scored_goals, suff_goals, points]}
I was trying to do something like this:
tabela = {}
for teams,goals in dict_results.items():
win = 0
draw = 0
loss = 0
scored_goals = 0
suff_goals = 0
points = 0
print(teams,goals)
if teams[0] not in tabela:
tabela[teams[0]] = [win, draw, loss, scored_goals, suff_goals, points]
if goals[0] > goals[1]:
#teams[0] update
win = 1
scored_goals = goals[0]
suff_goals = goals[1]
points = 3
tabela[teams[0]].update([win,draw,loss,scored_goals,suff_goals,points])
elif goals[0] == goals[1]:
draw = 1
scored_goals = goals[0]
suff_goals = goals[1]
points = 1
tabela[teams[0]].update([win,draw,loss,scored_goals,suff_goals,points])
tabela[teams[1]].update([win,draw,loss,scored_goals,suff_goals,points])
elif goals[0] < goals[1]:
...
but the values are kept at 0 (they don't actually update) which makes sense really since every iteration I'm basically "resetting" the values and even if the values were being updated the way I'm trying to do, I wouldn't really get cumulative values.
How could I do it so that I can compare the goals for each team and update the results accordingly? For example, in the tuple ('FCP','SLB') the goals scored for 'FCP' were 1 and goals scored for SLB were 4, which means that 'FCP' suffered 4 goals, scored 1 goal, got no points, increased the loss by 1 and 'SLB' increase the win by 1, suffered 1 goal, scored 4 goals and got 3 points (without using dictionary or list comprehensions)
Thank you in advance!
CodePudding user response:
As an addition, you are looping over both teams but you are recording only the result for team one. You can get both teams with an extra for loop.
tabela = {}
for teams,goals in dict_results.items():
for iter in range(len(teams)):
team = teams[iter]
win = 0
draw = 0
loss = 0
scored_goals = 0
suff_goals = 0
points = 0
if iter == 0:
n = 0
m = 1
else:
n = 1
m = 0
if team not in tabela:
tabela[team] = [win, draw, loss, scored_goals, suff_goals, points]
if goals[n] > goals[m]:
win, draw, loss, scored_goals, suff_goals, points = tabela[team]
win = 1
scored_goals = goals[n]
suff_goals = goals[m]
points = 3
tabela[team] = [win,draw,loss,scored_goals,suff_goals,points]
elif goals[n] < goals[m]:
win, draw, loss, scored_goals, suff_goals, points = tabela[team]
win = 0
scored_goals = goals[n]
suff_goals = goals[m]
points = 0
tabela[team] = [win,draw,loss,scored_goals,suff_goals,points]
elif goals[n] == goals[m]:
win, draw, loss, scored_goals, suff_goals, points = tabela[team]
win = 0
scored_goals = goals[n]
suff_goals = goals[m]
points = 1
tabela[team] = [win,draw,loss,scored_goals,suff_goals,points]
print(tabela)
>>{'FCP': [2, 0, 0, 13, 13, 6], 'SLB': [2, 0, 0, 11, 13, 6], 'SCP': [2, 0, 0, 13, 11, 6]}
Depending on your iteration iter, n and m switch numbers. This way the if statement checks the scores for team one on iteration 1 and the scores for team two on iteration 2.
CodePudding user response:
You're right, it looks like you are indeed "resetting" the values on every iteration. In Python, the .update function doesn't add values, it just replaces the dictionary entry. You can still use tabela[team].update, you just need to add to the previous value yourself.
Here's a code example of how to do that:
...
if goals[0] > goals[1]:
#teams[0] update
win, draw, loss, scored_goals, suff_goals, points = tabela[teams[0]]
win = 1
scored_goals = goals[0]
suff_goals = goals[1]
points = 3
tabela[teams[0]].update([win,draw,loss,scored_goals,suff_goals,points])
elif goals[0] == goals[1]:
...
elif goals[0] < goals[1]:
...
