Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting occurrences in a Python list

Tags:

python

I have a list of integers; for example:

l = [1, 2, 3, 4, 4, 4, 1, 1, 1, 2]

I am trying to make a list of the three elements in l with the highest number of occurrences, in descending order of frequency. So in this case I want the list [1, 4, 2], because 1 occurs the most in l (four times), 4 is next with three instances, and then 2 with two. I only want the top three results, so 3 (with only one instance) doesn't make the list.

How can I generate that list?

like image 241
sammy_jacob Avatar asked May 18 '11 14:05

sammy_jacob


3 Answers

Use a collections.Counter:

import collections
l= [1 ,2 ,3 ,4,4,4 , 1 ,1 ,1 ,2]

x=collections.Counter(l)
print(x.most_common())
# [(1, 4), (4, 3), (2, 2), (3, 1)]

print([elt for elt,count in x.most_common(3)])
# [1, 4, 2]

collections.Counter was introduced in Python 2.7. If you are using an older version, then you could use the implementation here.

like image 178
unutbu Avatar answered Oct 11 '22 23:10

unutbu


l_items = set(l) # produce the items without duplicates
l_counts = [ (l.count(x), x) for x in set(l)]
# for every item create a tuple with the number of times the item appears and
# the item itself
l_counts.sort(reverse=True)
# sort the list of items, reversing is so that big items are first
l_result = [ y for x,y in l_counts ]
# get rid of the counts leaving just the items
like image 31
Winston Ewert Avatar answered Oct 11 '22 23:10

Winston Ewert


from collections import defaultdict
l= [1 ,2 ,3 ,4,4,4 , 1 , 1 ,1 ,2]
counter=defaultdict(int)
for item in l:
    counter[item]+=1

inverted_dict = dict([[v,k] for k,v in counter.items()])

for count in sorted(inverted_dict.keys()):
    print inverted_dict[count],count

This should print out the most frequents items in 'l': you would need to restrict to the first three. Be careful when using the inverted_dict there (that is the keys and values gets swapped): this will result in an over-write of values (if two items have identical counts, then only one will be written back to the dict).

like image 29
monojohnny Avatar answered Oct 11 '22 23:10

monojohnny