Note that there are some explanatory texts on larger screens.

plurals
  1. POSubtracting by Dictionary Values from Iterated Total in For Loop Python
    text
    copied!<p>Working in Python 2.7.</p> <p>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:</p> <pre><code>NL = {'Phillies': [662, 476], 'Braves': [610, 550], 'Mets': [656, 687]} </code></pre> <p>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.</p> <p>I first tried something along the lines of this:</p> <pre><code>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] </code></pre> <p>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.</p> <p>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.</p> <p>Is there a way I can return the team_removed runs for all the teams in the dictionary?</p> <p>Thanks for any help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload