Created
May 8, 2018 14:44
-
-
Save Martin91/0791698b4e9e400921f3963ace65f41f to your computer and use it in GitHub Desktop.
python sort dictionary with value in desc order
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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