使用 Counter 在新行上显示字典的每个元素

Display each element of the dictionary on a new line working with Counter

我有一个随机颜色的数组。有重复的元素。例如:

mass = ['white','black','white'...]

我需要对它们进行计数,所以我决定使用计数器:

mass_count = Counter(mass)
print (mass_count)

What I've got

我想在新行上显示字典的每个元素。我用过那个代码:

keys = [k for k in mass_count]
for i, key in enumerate(keys):      
    print("\n" + str(list(mass_count.keys())[i]) + ": " + str(mass_count[keys[i]]))

What I've got #2

一切如我所愿,除了一件事:在我的第一个结果中,所有元素都从最大到最小显示,但第二个结果违反了这个顺序。所以我想要:

白色:22

黑色:14

天蓝色:6

...

如何实现?谢谢。

你可以使用 Counter.most_common() method

for color, value in mass_count.most_common():
    print(f"{color}: {value}")