CheckIO (Python) > The Most Wanted Letter 試してみた > もっとも頻度が高いアルファベットの探索 > collectionsのCounter使用
の課題の関連情報を見ていて、以下のsuggestionを受けた。
Let's suppose you got a list like this (maybe dict.items()):
[("a", 2), ("b", 4), ("c", 5), ....]
1
Then you can get the answer with advanced sorting:
sorted(array, key=lambda x: -x[1], x[0])
1
And the required letter will be the first.
sorted()は使ったことがなかった。
参考: http://akiyoko.hatenablog.jp/entry/2014/09/26/235300
alist = [ ("b", 2), ("z", 5), ("a", 5), ("c", 4) ]
alist = sorted(alist, key=lambda x: (-x[1], x[0]))
print(alist)
run
[('a', 5), ('z', 5), ('c', 4), ('b', 2)]