Reputation: 37
I was given this list with historical scores of games from a popular college team (Oregon Ducks):
[
'1916-10-07,1916,Willamette,97,0\n',
'1916-10-14,1916,Multnomah A.C.,28,0\n',
'1916-10-21,1916,California,39,14\n',
'1916-11-04,1916,Washington,0,0\n',
'1916-11-11,1916,Washington State,12,3\n',
]
I wrote code to extract the season, duck's scores, and allowed scores and put them in a dictionary:
def parse_football_data(lst):
DUCKS = {
'season': [int(i.split(',')[1]) for i in lst],
'scored': [int(i.split(',')[3]) for i in lst],
'allowed': [int(i.split(',')[4].strip()) for i in lst],
}
return DUCKS
Which has this output:
{'season': [1916, 1916, 1916, 1916, 1916],
'scored': [97, 28, 39, 0, 12],
'allowed': [0, 0, 14, 0, 3]}
I now need to write a function def total_by_year(games, year):
which will give me the sum of scores for the input year, and the opposing's team allowed scores in this format:
total_by_year(dct, 1916)
(51, 17)
when I input this using my previous function:
dct = parse_football_data([
'1916-10-21,1916,California,39,14\n',
'1916-11-04,1916,Washington,0,0\n',
'1916-11-11,1916,Washington State,12,3\n',
'1917-11-17,1917,California,21,0\n',
'1917-11-29,1917,Oregon State,7,14\n'
])
How can I match the year to the "season" and "allowed" keys in the dictionary?
Upvotes: 0
Views: 45
Reputation: 4455
This sounds a lot like homework. Nonetheless, consider this example implementation of total_by_year().
def total_by_year(dct, year):
scored = 0
allowed = 0
for (index,season) in enumerate( dct['season'] ):
if season == year:
scored += dct['scored'][index]
allowed += dct['allowed'][index]
return ( scored, allowed )
Upvotes: 1