简体   繁体   English

计算Python列表中的出现次数

[英]Counting occurrences in a Python list

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. 我试图按照频率的降序列出具有最高出现次数的l中的三个元素。 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. 因此,在这种情况下,我想要列表[1, 4, 2] ,因为1发生在l (四次)中最多, 4表示下三个实例,然后2两个。 I only want the top three results, so 3 (with only one instance) doesn't make the list. 我只想要前三个结果,所以3 (只有一个实例)不会列出。

How can I generate that list? 我该如何生成该列表?

Use a collections.Counter : 使用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. collections.Counter是在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. 这应打印出'l'中最常用的项目:您需要限制前三项。 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). 在那里使用inverted_dict时要小心(即键和值被交换):这将导致值的重写(如果两个项具有相同的计数,则只有一个将被写回到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). for循环对于在不丢失顺序的情况下进行统一是必要的(可能有更好的方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM