How to sort a Python dict's keys by value

后端 未结 5 2095
逝去的感伤
逝去的感伤 2020-12-15 05:35

I have a dict that looks like this

{ \"keyword1\":3 , \"keyword2\":1 , \"keyword3\":5 , \"keyword4\":2 }

And I would like to convert it DESC and

5条回答
  •  孤城傲影
    2020-12-15 06:15

    I would come up with something like this:

    [k for v, k in sorted(((v, k) for k, v in theDict.items()), reverse=True)]
    

    But KennyTM's solution is much nicer :)

提交回复
热议问题