繰り返しの名前を持つ一連のペア(名前、スコア)があります。それぞれの名前の最大スコアを取得したいと思います。名前ラベル自体は、最終結果ではオプションです。これは実用的な実装です:
from collections import defaultdict
scores = (('eyal', 76), ('alex', 50), ('oded', 90), ('eyal', 100), ('alex', 99))
distinct = defaultdict(set)
for score in scores:
distinct[score[0]].add(score[1])
max_scores = [max(distinct[k]) for k in distinct]
print (max_scores)
私は疑問に思っています、これは辞書内包表記を使用して1つのステップで実行できますか?