CheckIO (Python)> The Most Wanted Letter I tried> Searching for the most frequent alphabets> Using Counter in collections Looking at the related information on the subject, I received the following suggestions.
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.
I have never used sorted ().
Reference: http://akiyoko.hatenablog.jp/entry/2014/09/26/235300
http://ideone.com/GoQ3Md
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)]
Recommended Posts