可以使用Python内置的Counter类,以字典的形式返回列表中元素的计数结果。然后,遍历字典,找到出现次数最多的数字。
代码实现如下:
from collections import Counter
def highest_occuring_number(num): count_dict = Counter(num) max_count = max(count_dict.values()) result = list(filter(lambda x: count_dict[x]==max_count, count_dict.keys())) return result
num = [1,2,3,2,1,2,2,4,5,4,4,4] result = highest_occuring_number(num) print(result)
输出:[2, 4]
下一篇:编写Python函数只删除奇数