Finding the most common elements in an iterable in Python

How to find the most common elements in an iterable in Python or Django

collections.Counter lets you find the most common

# collections.Counter lets you find the most common
# elements in an iterable:

>>> import collections
>>> c = collections.Counter('helloworld')

>>> c
Counter({'l': 3, 'o': 2, 'e': 1, 'd': 1, 'h': 1, 'r': 1, 'w': 1})

>>> c.most_common(3)
[('l', 3), ('o', 2), ('e', 1)]

 

Please follow and like us:
0

Leave a Reply

Your email address will not be published. Required fields are marked *