首先按键排序字典,然后按值排序 [英] Sort dictionary first by key, then by value
本文介绍了首先按键排序字典,然后按值排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想对可能以int或tuple作为键的字典进行双重排序.
I want to double sort a dictionary that might have int or tuple as keys.
d = {33:1, 44:1, (0,5):1 12:2, (1,2):2}
我希望它首先按其值排序,然后按其键排序(如果可能,如果一个是元组,第二个是int,则不要).
I want it sorted first by its values and secondly by its keys (if possible, not if one is tuple, and second is int).
有人可以帮我吗?
预先感谢
推荐答案
或者只是
sorted(d.items(), key=lambda v: v[::-1])
由于OP要求将结果作为字典
Since OP requires the result as a dictionary
OrderedDict(sorted(d.items(), key=lambda v: v[::-1]))
这篇关于首先按键排序字典,然后按值排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文