Skip to content

Instantly share code, notes, and snippets.

@eyaltrabelsi
Created July 6, 2019 05:01
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 eyaltrabelsi/ccbd61d6e2ec40193719c0ba984cb382 to your computer and use it in GitHub Desktop.
Save eyaltrabelsi/ccbd61d6e2ec40193719c0ba984cb382 to your computer and use it in GitHub Desktop.
Python tricks most commont elements in an iterable
import collections
>>> A = collections.Counter([1, 1, 2, 2, 3, 3, 3, 3, 4, 5, 6, 7])
>>> A
Counter({3: 4, 1: 2, 2: 2, 4: 1, 5: 1, 6: 1, 7: 1})
>>> A.most_common(1)
[(3, 4)]
>>> A.most_common(3)
[(3, 4), (1, 2), (2, 2)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment