Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pythonic way to find all elements with the highest frequency? [duplicate]

I have a list such as this:

lst = [1, 3, 5, 1, 5, 6, 1, 1, 3, 4, 5, 2, 3, 4, 5, 3, 4]

I would like to find all the elements which occur most frequently. So I would like:

most = [1, 3, 5]

1, 3, and 5 would occur the most, which is 4 times. What's a fast, pythonic way to do this? I tried methods shown here:

How to find most common elements of a list?.

But it only gives me the top 3, I need all elements. Thank you.

like image 575
Arjun Vasudevan Avatar asked Dec 19 '22 09:12

Arjun Vasudevan


2 Answers

With collections.Counter and a list comprehension:

from collections import Counter

lst = [1, 3, 5, 1, 5, 6, 1, 1, 3, 4, 5, 2, 3, 4, 5, 3, 4]
r = [x for x, _ in Counter(lst).most_common(3)]
print(r)
# [1, 3, 5]

You can generalize for values with highest count by using max on the counter values:

c = Counter(lst)
m = max(c.values())
r = [k for k in c if c[k] == m]
print(r)
# [1, 3, 5]

For large iterables, to efficiently iterate through the counter and stop once the required items have been taken, you can use itertools.takewhile with most_common without any parameters:

from itertools import takewhile

c = Counter(lst)
m = max(c.values())
r = [x for x, _ in takewhile(lambda x: x[1]==m, c.most_common())] 
print(r)
# [1, 3, 5]

You gain by not having to iterate through all the items in the counter object, although there is some overhead with having to sort the items using most_common; so I'm sure if this absolutely efficient after all. You could do some experiments with timeit.

like image 94
Moses Koledoye Avatar answered Dec 28 '22 06:12

Moses Koledoye


You can also get the same result with groupby from itertools module and list comprehension in this way:

from itertools import groupby

a = [1, 3, 5, 1, 5, 6, 1, 1, 3, 4, 5, 2, 3, 4, 5, 3, 4]
most_common = 3
final = [k for k,v in groupby(sorted(a), lambda x: x) if len(list(v)) > most_common]

Output:

print(final)
>>> [1, 3, 5]
like image 21
Chiheb Nexus Avatar answered Dec 28 '22 06:12

Chiheb Nexus