我需要编写一个函数,分别查看包含两个团队及其游戏分数的嵌套列表。该列表包含多个匹配项,我希望输出是一个嵌套列表,其中包含所有团队名称和赢得的游戏数量。该列表如下所示:
L = [['Patriots', 'Giants', '3', '1'], ['Steelers', 'Patriots', '1', 2'], ['Giants', 'Steelers', '3', '5']]
所以在上面的列表中,前两个元素是团队名称,第三和第四个元素是他们在比赛中得分。但是,这个列表比这个要大得多,还有更多的团队。输出看起来像这样:
finalList = [['Patriots', 2], ['Giants', 0], ['Steelers', 1]]
因为爱国者赢了两场比赛,巨人队赢了零比赛,钢人队赢了一场比赛。
我已经尝试了以下代码,但它不起作用而且我被卡住了。
def gamesWon():
for i in L:
count = 0
if i[2]>i[3]:
count += 1
i.append(count)
答案 0 :(得分:3)
您可以使用defaultdict
:
from collections import defaultdict
# initialize the result as a defaultdict with default value of 0
result = defaultdict(lambda : 0)
for t1,t2,s1,s2 in L:
if int(s1) > int(s2):
result[t1] += 1
elif int(s2) > int(s1):
result[t2] += 1
result
# defaultdict(<function __main__.<lambda>>, {'Patriots': 2, 'Steelers': 1})
请注意,即使在结果中,得分为零的团队也会丢失,但如果您拨打result[team]
,则会给您零。
答案 1 :(得分:2)
您可以使用defaultdict
from collections import defaultdict
L = [['Patriots', 'Giants', '3', '1'], ['Steelers', 'Patriots', '1', '2'], ['Giants', 'Steelers', '3', '5']]
D = defaultdict(int)
for match in L:
team1, team2, score1, score2 = match
D[team1] # make sure the team exist in the dict even if it never wins a match
D[team2] # make sure the team exist in the dict even if it never wins a match
if int(score1) > int(score2):
D[team1] += 1
if int(score2) > int(score1):
D[team2] += 1
如果您绝对需要,可以轻松地将D
转换为列表...
答案 2 :(得分:0)
或者,您可以使用Counter
,类似于dict:
import collections as ct
L = [
['Patriots', 'Giants', '3', '1'],
['Steelers', 'Patriots', '1', '2'],
['Giants', 'Steelers', '3', '5'],
['Giants', 'Patriots', '1', '1'] # tie
]
def count_wins(games):
"""Return a counter of team wins, given a list of games."""
c = ct.Counter()
for team1, team2, s1, s2 in games:
c[team1] += 0
c[team2] += 0
if int(s1) == int(s2):
continue
elif int(s1) > int(s2):
c[team1] += 1
else:
c[team2] += 1
return c
season = count_wins(L)
season
# Counter({'Giants': 0, 'Patriots': 2, 'Steelers': 1})
后一个代码为新条目提供了默认的零增量并处理了关系:
L_tie = [['Cowboys', 'Packers', '3', '3']]
game = count_wins(L_tie)
game
# Counter({'Cowboys': 0, 'Packers': 0})
计数器有一些有用的方法可以找到顶级团队:
season.most_common(2)
# [('Patriots', 2), ('Steelers', 1)]
计数器很灵活。您可以轻松更新计数器:
season.update(game)
season
# Counter({'Cowboys': 0, 'Giants': 0, 'Packers': 0, 'Patriots': 2, 'Steelers': 1})
您还可以add (subtract and perform set operations with)其他计数器:
L_last = [['49ers', 'Raiders', '7', '10'], ['Packers', 'Patriots', '3', '7']]
last_season = count_wins(L_last)
season + last_season
# Counter({'Patriots': 3, 'Raiders': 1, 'Steelers': 1})
更新:另请参阅this related answer了解Counter
/ generator表达式变体。
答案 3 :(得分:0)
ll = [['Patriots', 'Giants', '3', '1'], ['Steelers', 'Patriots', '1', '2'], ['Giants', 'Steelers', '3', '5']]
teamStatus = {}
for l in ll:
team1,team2,team1_score,team2_score = l
if team1 not in teamStatus:
teamStatus[team1] = 0
if team2 not in teamStatus:
teamStatus[team2] = 0
if int(team1_score) > int(team2_score):
teamStatus[team1] += 1
else:
teamStatus[team2] += 1
print(teamStatus)
<强> RESULT 强>
{'Patriots': 2, 'Giants': 0, 'Steelers': 1}