python - 如何使用python查找列表中最大数字出现的次数?

标签 python list max

例如 如果列表是:list = [1, 2, 3, 4, 4] 如何计算数字 4 出现的次数,而不指定它。使用max(list)或类似的东西。

最佳答案

使用max尝试一下:

l = [1,2,3,4,4]
num = max(l, key=lambda x:l.count(x))  # num will be 4

可以得到num的计数。

l.count(num)  # this wil returns 2

另一方面,正如@buddemat所说(Here),最好:

from collections import Counter      
                                                                                                                                                                                                                                         
l = [1,2,3,4,4]
c = Counter(l)  
c.most_common()[0]

会给你

(4,2) # 4 is a maximum number and 2 is number of occurrence.

另请注意:

DO NOT use list as a variable name, list is predefined in python and you will override its own functionality.

关于python - 如何使用python查找列表中最大数字出现的次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64753308/

相关文章:

list - Flutter:删除列表中的项目反射(reflect)另一个列表

python - 如何在 2d python 列表中找到最大数

sql - 使用列的最大值从oracle sql表中过滤行

elasticsearch - Lucene和Elasticsearch超出了文档限制

python - 通过鼠标移动旋转当前播放的动画

python - 编码Bat sum67 : why is this solution wrong?

Python C接口(interface),不同模块共享静态变量?

python3从列表列表中总结元素

python - pandas的combine_first导致更多的行数

oop - OCaml 结构类型和列表