Skip to content

Instantly share code, notes, and snippets.

@Martin91
Created May 8, 2018 14:44
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 Martin91/0791698b4e9e400921f3963ace65f41f to your computer and use it in GitHub Desktop.
Save Martin91/0791698b4e9e400921f3963ace65f41f to your computer and use it in GitHub Desktop.
python sort dictionary with value in desc order
import collections
mydict = {
'a': 49023,
'b': 39201,
'c': 49021,
'd': 90201,
'e': 49012,
'f': 49401,
'g': 10039,
'h': 23131,
'i': 39293,
'j': 52391,
'k': 58123,
'l': 50130,
}
d = collections.Counter(mydict)
for k, v in d.most_common(5):
print '%s: %s' % (k, v)
# d: 90201
# k: 58123
# j: 52391
# l: 50130
# f: 49401
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment