How to sort a Python dict's keys by value

后端 未结 5 2088
逝去的感伤
逝去的感伤 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:20

    i always did it this way....are there advantages to using the sorted method?

    keys = dict.keys()
    keys.sort( lambda x,y: cmp(dict[x], dict[y]) )
    

    whoops didnt read the part about not using lambda =(

提交回复
热议问题