LoginSignup
2
2

More than 5 years have passed since last update.

Pythonでリスト内要素を要素、重複数として重複数でソートして出力

Posted at

Pythonを使ってリスト内の文字列要素の重複を調べ、その結果を重複数の降順で要素、重複数として出力するためのスクリプトです。
内包表記で表現しています。

data = ['a', 'b', 'c', 'd', 'b', 'c', 'd', 'b', 'c', 'b']
result = sorted({i: data.count(i) for i in set(data)}.items(), key=lambda x: x[1], reverse=True)
print(result)

>>> [('b', 4), ('c', 3), ('d', 2), ('a', 1)]
2
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
2
2