例如
如果列表是: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/