from collections import Counter
def myFunction(myDict):
myMax = 0 # Keep track of the max frequence
myResult = [] # A list for return
for key in myDict:
print('The key is', key, ', The count is', myDict[key])
print('My max is:', myMax)
# Finding out the max frequence
if myDict[key] >= myMax:
if myDict[key] == myMax:
myMax = myDict[key]
myResult.append(key)
# Case when it is greater than, we will delete and append
else:
myMax = myDict[key]
del myResult[:]
myResult.append(key)
return myResult
foo = ['1', '1', '5', '2', '1', '6', '7', '10', '2', '2']
myCount = Counter(foo)
print(myCount)
print(myFunction(myCount))
输出:
^{pr2}$
我写了这个简单的程序,我想它也可以工作。在搜索之前,我不知道most_common()函数。我认为这将返回尽可能多的最频繁元素,它通过比较max frequency元素来工作,当我看到一个更频繁的元素时,它将删除结果列表,并追加一次;或者如果它是相同的频率,它只需追加到它。一直走,直到整个计数器被遍历。在