Python取出数组中重复次数最多的数
def max_list(lt): temp = 0 for i in lt: if lt.count(i) > temp: max_ele = i temp = lt.count(i) return "重复次数最多元素为%s,重复次数为%d次" % (max_ele, temp) n = ['a','b','a','c','d','a'] print(max_list(n)) 重复次数最多元素为a,重复次数为3次
def max_list(lt): temp = 0 for i in lt: if lt.count(i) > temp: max_ele = i temp = lt.count(i) return "重复次数最多元素为%s,重复次数为%d次" % (max_ele, temp) n = ['a','b','a','c','d','a'] print(max_list(n)) 重复次数最多元素为a,重复次数为3次