Working in Python 2.7.
I have a dictionary with team names as the keys and the values are contained in lists. The first value is the amount of runs the team scored, the second is runs allowed:
NL = {'Phillies': [662, 476], 'Braves': [610, 550], 'Mets': [656, 687]}
I have a function that iterates over each key and provides the total runs scored and allowed for the dictionary as a whole. I would also like to be able to subtract each individual team from the total, and create a league minus team value.
I first tried something along the lines of this:
def Tlog5(league, league_code):
total_runs_scored = 0
total_runs_allowed = 0
for team, scores in league.iteritems():
total_runs_scored += float(scores[0])
total_runs_allowed += float(scores[1])
team_removed_runs = total_runs_scored - scores[0]
Unfortunately, that appeared to be subtracting from only the values that had already been iterated instead of the complete total. So, for the first team in the dictionary, team_removed_runs was 0, for the second team it was the the total runs for of the first two teams minus the second teams total (leaving only the first teams total.
I tried to move the team_removed_runs = total_runs_scored – scores[0] out of the for loop, but then I only got a value for the last team in the dictionary.
Is there a way I can return the team_removed runs for all the teams in the dictionary?
Thanks for any help!
If you want the
team_removed_runsfor every team in the dictionary, you’ll need to go back through the dictionary and compute the total number of runs minus the runs for each team. Something likeThis will use the final values of
total_runs_scoredandtotal_runs_allowedfor the entire league, and then subtract each teams values from that total, storing the result in the dictionaryteam_removed_runs. So if you wanted the value for the league total less the Phillies, you could find this at