Python 如何在字典中找到N个重复次数最多的元素?

Python 如何在字典中找到N个重复次数最多的元素?,python,list,dictionary,Python,List,Dictionary,以下是我得到的: dict = {"a":2, "b":10, "c":5, "d":10, "e":6, "f":3, "g":4} 我想找到这本词典中重复次数最多的3个元素,因此输出为: {"b":10, "d":10, "e":6} 你也可以通过排序来实现这一点。为什么6是第三位而不是第二位?到目前为止你尝试了什么?dict(collections.Counter(my_dict)。most_common(3))most_common: import operator sorted

以下是我得到的:

dict = {"a":2, "b":10, "c":5, "d":10, "e":6, "f":3, "g":4} 
我想找到这本词典中重复次数最多的3个元素,因此输出为:

{"b":10, "d":10, "e":6}

你也可以通过排序来实现这一点。

为什么6是第三位而不是第二位?到目前为止你尝试了什么?
dict(collections.Counter(my_dict)。most_common(3))
most_common
import operator 
sorted(a.items(), key=operator.itemgetter(1), reverse=True)[:3]