繁体   English   中英

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

[英]Sum values from a list, selected by another 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 个列表后对它们进行排序是第一步。

最好的方法是什么?

这是使用字典的一种方法:

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

使用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
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}

你可以用熊猫做到这一点:

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

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

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

这是基于 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}

无需在这里汇总列表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM