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