开发者

Counting occurrences in a Python list

开发者 https://www.devze.com 2023-03-07 00:01 出处:网络
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 frequ

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 onl开发者_C百科y want the top three results, so 3 (with only one instance) doesn't make the list.

How can I generate that list?


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.


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


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).


Without using collections:

a = reversed(sorted(l,key=l.count))
outlist = []
for element in a:
  if element not in outlist:
    outlist.append(element)

The first line gets you all the original items sorted by count.

The for loop is necessary to uniquify without losing the order (there may be a better way).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号