1 回答
TA贡献1784条经验 获得超7个赞
根据提供的有关如何对解决方案进行排名的信息,可以:
from collections import defaultdict
matches = [("Team D", "Team A"), ("Team E", "Team B"), ("Team T", "Team B"),
("Team T", "Team D"), ("Team F", "Team C"), ("Team C", "Team L"),
("Team T", "Team F")]
def winning_list(mathces):
scores = defaultdict(int)
for fst, snd in matches:
scores[fst] += 1
scores[snd] -= 1
return sorted(scores.items(), key=lambda e: e[1], reverse=True)
ranking = winning_list(matches)
print(ranking)
为了使它更简单,我们可以使用collections.Counter
from collections import Counter
def winning_list2(mathces):
scores = Counter()
for fst, snd in matches:
scores[fst] += 1
scores[snd] -= 1
return scores.most_common()
添加回答
举报