列表中的总和值,由另一个列表选择

时间:2015-11-26 06:36:08

标签: python list

我有团队名称和分数的数据:

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,

但信息存储在2个列表中:

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,]

我需要A队得分和B队得分的总和。

也许在压缩2个列表之后对它们进行排序是第一步。

找出最好的方法是什么?

6 个答案:

答案 0 :(得分:2)

这是一种方法,用字典:

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

使用您的示例:

>>> 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

答案 1 :(得分:2)

使用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(list)
for k, s in zip(List_team, List_score):
    result[k].append(s)

然后,您可以使用这些数据执行任何操作,例如将值传递给sum()

>>> sum(result['Team A'])
67

答案 2 :(得分:2)

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}

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}

答案 3 :(得分:1)

你可以用熊猫来做:

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

答案 4 :(得分:1)

使用理解将默认字典设置为零,然后逐个位置填充。其他解决方案可能更具惯用性,但这个解决方案似乎最具人性化。

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

结果:

{'Team A': 67, 'Team C': 25, 'Team B': 39}

更新:一次只获得一个团队的分数:

print scores['Team A'] # Just Team A

结果:

67

答案 5 :(得分:1)

这是基于TigerhawkT3的答案,我发现最好使用int代替list作为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

结果会退还{'Team A': 67, 'Team B': 39, 'Team C': 25}

无需在此处汇总列表。