Python question. Let's say I am gonna have various lists, like this,
[1, 5, 3, 1, 9, 10, 1, 20]
[1, 5]
[1, 2, 3, 1, 7, 10, 1, 2, 8, 21]
[1, 5, 6, 4, 0, 16, 1, 5, 6, 4, 0, 16]
The numbers represent scores from sports games where Team 1 is the first half of a list, Team 2 is the second half of the list. The last number from the first half of the list (Team 1 scores) is always the game total. The second to last number from the first half of the list is always the game overtime score. The first item on the list is the score from the first period. The second item on the list is the score from the second period. Etc. So for example, [1,5] would mean Team 1 had a total score of 1, and Team 2 had a total score of 5. Another example, [1,5,3,9,9,10,1,20] would mean Team 1 scored 1 point in the first period, 5 points in the second period, 3 points in overtime, and 9 points total, Team 2 scored 9 points in the first period, 10 points in the second period, 1 point in overtime, and 20 points total.
I want to make a function that sticks these numbers from a list, into a dictionary, and it should only fill the dictionary key if there is a value in the list for it.
score['sec_away_team_1'], score['sec_away_team_2'], score['sec_away_team_3'], score['sec_away_team_4'], score['sec_away_team_ot'], score['sec_away_team_total'],
score['sec_home_team_1'], score['sec_home_team_2'], score['sec_home_team_3'], score['sec_home_team_4'], score['sec_home_team_ot'], score['sec_home_team_total']
One way to do this would be to make a function with a lot of if statements, if I know how long the list is, I can do different scenarios for how to unpack the list, but since I will be unpacking lists of various sizes I think it would be ugly to make so many if statements...How can this be done with a generic function that either yields items from the list, or uses them and pops them out, or some other way?
This function does what you want:
def scores_to_dict(scores):
d = {}
team_1 = ('team_1', scores[:len(scores)/2])
team_2 = ('team_2', scores[len(scores)/2:])
teams = [team_1, team_2]
for team, scores in teams:
for idx, score in enumerate(reversed(scores)):
if idx == 0:
d['%s_total' % team] = score
elif idx == 1:
d['%s_overtime' % team] = score
else:
d['%s_round_%s' % (team, len(scores)-idx)] = score
return d
Testing it:
scores = [[1, 5, 3, 1, 9, 10, 1, 20],
[1,5],
[1, 2, 3, 1, 7, 10, 1, 2, 8, 21],
[1, 5, 6, 4, 0, 16, 1, 5, 6, 4, 0, 16]]
from pprint import pprint
for score in scores:
print score
pprint(scores_to_dict(score))
print '--'
Output:
>>>
[1, 5, 3, 1, 9, 10, 1, 20]
{'team_1_overtime': 3,
'team_1_round_1': 1,
'team_1_round_2': 5,
'team_1_total': 1,
'team_2_overtime': 1,
'team_2_round_1': 9,
'team_2_round_2': 10,
'team_2_total': 20}
--
[1, 5]
{'team_1_total': 1, 'team_2_total': 5}
--
[1, 2, 3, 1, 7, 10, 1, 2, 8, 21]
{'team_1_overtime': 1,
'team_1_round_1': 1,
'team_1_round_2': 2,
'team_1_round_3': 3,
'team_1_total': 7,
'team_2_overtime': 8,
'team_2_round_1': 10,
'team_2_round_2': 1,
'team_2_round_3': 2,
'team_2_total': 21}
--
[1, 5, 6, 4, 0, 16, 1, 5, 6, 4, 0, 16]
{'team_1_overtime': 0,
'team_1_round_1': 1,
'team_1_round_2': 5,
'team_1_round_3': 6,
'team_1_round_4': 4,
'team_1_total': 16,
'team_2_overtime': 0,
'team_2_round_1': 1,
'team_2_round_2': 5,
'team_2_round_3': 6,
'team_2_round_4': 4,
'team_2_total': 16}
--