【发布时间】:2020-01-26 02:09:39
【问题描述】:
有没有办法将列表中出现次数最多的元素的列表输出到列表中,或者如果出现平局,则将所有出现次数最多的元素输出到列表中?
我想在不导入任何类的情况下解决这个问题!
例如,[5,4,3] 将输出 [5,4,3]。
或
[5,4,4,5] 将输出 [5,4]
我已经尝试过,max(set(list), key=list.count) 但对领带确实不起作用。
到目前为止我的工作:
test = ['test1', 'test2', 'test3']
dict = {}
for elements in test:
if elements in dict:
dict[elements] += 1
else:
dict[elements] = 0
dict[elements] += 1
print (dict)
【问题讨论】:
-
使用Counter
-
@ArkistarvhKltzuonstev 是的,已编辑!
-
您的第二个解决方案有什么问题?