Sorting dictionary keys in python [duplicate]

2019-01-07 03:02发布

This question already has an answer here:

I have a dict where each key references an int value. What's the best way to sort the keys into a list depending on the values?

4条回答
够拽才男人
2楼-- · 2019-01-07 03:46
[v[0] for v in sorted(foo.items(), key=lambda(k,v): (v,k))]
查看更多
仙女界的扛把子
3楼-- · 2019-01-07 03:49
my_list = sorted(dict.items(), key=lambda x: x[1])
查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-01-07 03:59

I like this one:

sorted(d, key=d.get)
查看更多
The star\"
5楼-- · 2019-01-07 04:01
>>> mydict = {'a':1,'b':3,'c':2}
>>> sorted(mydict, key=lambda key: mydict[key])
['a', 'c', 'b']
查看更多
登录 后发表回答