Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Created October 21, 2016 03:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanaikech/cfc419e6052a4f83c5e09dfd144797a9 to your computer and use it in GitHub Desktop.
Save tanaikech/cfc419e6052a4f83c5e09dfd144797a9 to your computer and use it in GitHub Desktop.
Pythonでリスト内要素を要素、重複数として重複数でソートして出力 ref: http://qiita.com/tanaike/items/5a94e9bb1154d1caefca
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)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment