使用Counter函数,遍历并不是按照Counter后的结果,而是按照Counter的内容进行遍历
如图:
from collections import Counter
dic = [1,4,5,3,1,3,4,4]
k = Counter(dic)
print(k)
结果
Counter({4: 3, 1: 2, 3: 2, 5: 1})
对其进行遍历:
from collections import Counter
dic = [1,4,5,3,1,3,4,4]
k = Counter(dic)
print(k)
for i,j in k.items():
print(i ,j)
结果
Counter({4: 3, 1: 2, 3: 2, 5: 1})
1 2
4 3
5 1
3 2
可以发现,遍历并不是从Counter后的字典中的4开始的,而是从dic中的1开始,并带着遍历字符的次数