5 maximum values in a python dictionary

前端 未结 5 1296
温柔的废话
温柔的废话 2020-11-28 23:59

I have a dictionary like this:

A = {\'a\':10, \'b\':843, \'c\': 39,.....}

I want to get the 5 maximum values of this dict and store a new d

5条回答
  •  有刺的猬
    2020-11-29 00:03

    For Python 3

    import operator
    dict(sorted(A.items(), key=operator.itemgetter(1), reverse=True)[:5])
    

提交回复
热议问题