1 回答

TA貢獻(xiàn)1784條經(jīng)驗 獲得超7個贊
根據(jù)提供的有關(guān)如何對解決方案進(jìn)行排名的信息,可以:
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()
添加回答
舉報