SHARE
TWEET

Untitled

a guest Jun 18th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. A = {'a':10, 'b':843, 'c': 39,.....}
  2.      
  3. max(A.iteritems(), key=operator.itemgetter(1))[0:]
  4.      
  5. newA = dict(sorted(A.iteritems(), key=operator.itemgetter(1), reverse=True)[:5])
  6.      
  7. max(A, key=A.get)
  8.      
  9. sorted(A, key=A.get, reverse=True)[:5]
  10.      
  11. import heapq
  12. heapq.nlargest(5, A, key=A.get)
  13.      
  14. >>> from collections import Counter
  15. >>> A = {'a' : 1, 'b' : 3, 'c' : 2, 'd' : 4, 'e' : 0, 'f' :5}
  16. >>> dict(Counter(A).most_common(5))
  17. {'a': 1, 'c': 2, 'b': 3, 'd': 4, 'f': 5}
  18.      
  19. dict(sorted(A.iteritems(), key=operator.itemgetter(1), reverse=True)[:5])
  20.      
  21. import operator
  22. dict(sorted(A.items(), key=operator.itemgetter(1), reverse=True)[:5])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top