python数字频率统计

如何用python统计一个整形序列中出现次数最多的整数以及出现的次数

#以空格隔开输入一串数字,按回车结束
s = input().split(" ")
max = 0
for i in range(len(s)):
    count = s.count(s[i])
    if max < count:
        max = count
        number = s[i]
print(number,max)

有帮助的话采纳一下哦!

1、自己写算法,使用哈希表(即python的dict类型)记录各个不同的数出现的次数,最后判断哪个次数最大即可
2、使用python的collections模块的counter计数器,如下

from collections import Counter
nums = [5,2,2,2]
counts = Counter(nums)
print(counts)  #输出 Counter({2: 3, 5: 1})
print(max(counts.keys(), key=counts.get))  # 输出2
s = input()
temp = {}
for x in s:
    temp[x] = temp.get(x, 0) + 1
temp = sorted(temp.items(), key=lambda x:x[1])
print(temp[-1])

可以建一个空字典,遍历序列,整数当key,value赋值为0,出现一次value +1
最后求最value最大值,输出对于的key 跟value