I have data of team names and scores:
Team A 9,
Team A 13,
Team B 24,
Team C 6,
Team A 15,
Team B 10,
Team C 19,
Team A 30,
Team B 5,
But the information is stored in 2 lists:
List_team = ['Team A', 'Team A', 'Team B', 'Team C',
'Team A', 'Team B', 'Team C', 'Team A', 'Team B']
List_score = [9, 13, 24, 6, 15, 10, 19, 30, 5,]
I need the sum of Team A's score and Team B's score.
Maybe to sort them after zipping the 2 lists is a first step.
What's the best way to find out?
This is one way to do that, with a dictionary:
teams = {}
teams_and_scores = zip(List_team,List_score) #this will pair each team with their score
for team, score in teams_and_scores:
if team in teams:
teams[team] += score #add score if team is already in dictionary
else:
teams[team] = score #add team to dictionary with their score
Using your example:
>>> List_team = ['Team A', 'Team A', 'Team B', 'Team C', 'Team A', 'Team B', 'Team C', 'Team A', 'Team B']
>>> List_score = [9, 13, 24, 6, 15, 10, 19, 30, 5,]
>>> teams = {}
>>> teams_and_scores = zip(List_team,List_score) #this will pair each team with their score
>>> for team, score in teams_and_scores:
if team in teams:
teams[team] += score #add score if team is already in dictionary
else:
teams[team] = score #add team to dictionary with their score
>>> teams
{'Team A': 67, 'Team B': 39, 'Team C': 25}
>>> teams['Team A']
67
Use a defaultdict
, which will populate a dictionary entry with the given key and a default value if it doesn't yet exist:
import collections
List_team = ['Team A', 'Team A', 'Team B', 'Team C', 'Team A', 'Team B', 'Team C', 'Team A', 'Team B']
List_score = [9, 13, 24, 6, 15, 10, 19, 30, 5]
result = collections.defaultdict(list)
for k, s in zip(List_team, List_score):
result[k].append(s)
You can then do any number of things with these data, such as passing a value to sum()
:
>>> sum(result['Team A'])
67
In [9]: result = {}
In [10]: for x in enumerate(List_team):
...: result.setdefault(x[1], 0)
...: result[x[1]] += List_score[x[0]]
...:
In [11]: result
Out[11]: {'Team A': 67, 'Team B': 39, 'Team C': 25}
or
In [15]: result = {}
In [16]: for x, y in zip(List_team, List_score):
...: result.setdefault(x, 0)
...: result[x] += y
...:
In [17]: result
Out[17]: {'Team A': 67, 'Team B': 39, 'Team C': 25}
You could do it with pandas:
import pandas as pd
List_team = ['Team A', 'Team A', 'Team B', 'Team C', 'Team A', 'Team B', 'Team C', 'Team A', 'Team B']
List_score = [9, 13, 24, 6, 15, 10, 19, 30, 5]
df = pd.DataFrame({'Team': List_team, 'Score': List_score})
df1 = df.groupby('Team').sum()
In [47]: df1
Out[47]:
Score
Team
Team A 67
Team B 39
Team C 25
Using a comprehension to populate a default dict set to zeroes, and then populating it position by position. Other solutions may be more idiomatic, but this one seemed most human.
List_team = ['Team A', 'Team A', 'Team B', 'Team C', 'Team A', 'Team B', 'Team C', 'Team A', 'Team B']
List_score = [9, 13, 24, 6, 15, 10, 19, 30, 5]
scores = { x : 0 for x in List_team }
for idx,team in enumerate(List_team):
scores[team] += List_score[idx]
print scores # all scores
Result:
{'Team A': 67, 'Team C': 25, 'Team B': 39}
Update: Get just one team's scores at a time:
print scores['Team A'] # Just Team A
Result:
67
This is based on TigerhawkT3's answer, I just find better to use int instead of list for the defaultdict.
import collections
List_team = ['Team A', 'Team A', 'Team B', 'Team C', 'Team A', 'Team B', 'Team C', 'Team A', 'Team B']
List_score = [9, 13, 24, 6, 15, 10, 19, 30, 5]
result = collections.defaultdict(int)
for k, s in zip(List_team, List_score):
result[k] += s
result would give back {'Team A': 67, 'Team B': 39, 'Team C': 25}
No need to sum lists here.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.