查找列表中最常用的元素

5年以前  |  阅读数:437 次  |  编程语言:Python 

此函数返回列表中出现频率最高的元素。

def most_frequent(list):
    return max(set(list), key = list.count)

numbers = [1, 2, 3, 2, 4, 3, 1, 3]
most_frequent(numbers) # 3

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8