LoginSignup
0
2

More than 5 years have passed since last update.

Python > 数値で並べ替えて英字で並び替え > sorted()使用

Last updated at Posted at 2017-07-25

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)]
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2