Python:列表中出现次数最多的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13707457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 09:25:30  来源:igfitidea点击:

Python: value that occurs the most in a list

pythonlist

提问by Keenan

I have a two list as follows

我有两个列表如下

x = ['a','a','b','c','b','a']

and

x = ['a','a','b','c','c','d']

I'm trying to find which values occur the most in each of these lists. This is what I have tried.

我试图找出每个列表中出现最多的值。这是我尝试过的。

def unique_values(output,input):
    for i in input:
        if i not in output:
            output.append(i)
k = []
for i in k:
    unique_values(k,x)
    y.remove(i)

I've gotten this far but I cant figure out how to stop the for i in k:before it removes all the values in the list.

我已经走了这么远,但我不知道如何for i in k:在它删除列表中的所有值之前停止。

采纳答案by Rohit Jain

You can use Countermodule from collections, if you want to find the occurrences of each element in the list: -

如果要查找列表中每个元素的出现次数,可以使用Countermodule from collections:-

>>> x = ['a','a','b','c','c','d']

>>> from collections import Counter
>>> count = Counter(x)
>>> count
Counter({'a': 2, 'c': 2, 'b': 1, 'd': 1})
>>> count.most_common()
[('a', 2), ('c', 2), ('b', 1), ('d', 1)]

So, the first two elements are most common in your list.

因此,前两个元素在您的列表中最常见。

>>> count.most_common()[0]
('a', 2)
>>> count.most_common()[1]
('c', 2)

or, you also pass parameter to most_common()to specify how many most-commonelements you want: -

或者,您还可以将参数传递给以most_common()指定most-common您想要的元素数量:-

>>> count.most_common(2)
[('a', 2), ('c', 2)]

Update : -

更新 : -

You can also find out the maxcount first, and then find total number of elements with that value, and then you can use it as parameter in most_common(): -

您也可以先找出max计数,然后找到具有该值的元素总数,然后您可以将其用作参数most_common():-

>>> freq_list = count.values()
>>> freq_list
[2, 2, 1, 1]
>>> max_cnt = max(freq_list)
>>> total = freq_list.count(max_cnt)

>>> most_common = count.most_common(total)
[('a', 2), ('c', 2)]

>>> [elem[0] for elem in most_common]
['a', 'c']

回答by Milton Segura

Here is another solution:

这是另一个解决方案:

max(zip((x.count(item) for item in set(x)), set(x)))

First, we get a collection containing no duplicate elements using set.

首先,我们使用 set 得到一个不包含重复元素的集合。

>>> set(x)
{'a', 'c', 'b'}

Then, we count how many times each element is in x. This will return a generator object, you can make it a list to see its values (by using "[ ... ]" instead of "( ... )" ), it would return [3, 1, 2].

然后,我们计算每个元素在 x 中的次数。这将返回一个生成器对象,您可以将其设为一个列表以查看其值(通过使用 "[ ... ]" 而不是 "( ... )" ),它将返回 [3, 1, 2]。

>>> (x.count(item) for item in set(x))

Then, we take the counts and pair it with the elements using zip. The number of occurrences first, for the next step. You can see its value by using list( ... ) on it, it would return [(3, 'a'), (1, 'c'), (2, 'b')].

然后,我们获取计数并使用 zip 将其与元素配对。首先出现的次数,用于下一步。您可以通过在其上使用 list( ... ) 来查看其值,它将返回 [(3, 'a'), (1, 'c'), (2, 'b')]。

>>> zip((x.count(item) for item in set(x)), set(x))

Finally, we find which of the pairs occurs most times using max.

最后,我们使用 max 找出哪些对出现的次数最多。

>>> max(zip((x.count(item) for item in set(x)), set(x)))
(3, 'a')

As for the second value, the solution is a bit lengthier. The above is used within a list comprehension:

至于第二个值,解决方案有点长。以上用于列表推导式:

>>> [mitem for mitem in zip((x.count(item) for item in set(x)),set(x)) if mitem[0] == max((x.count(item) for item in set(x)))]
[(2, 'a'), (2, 'c')]