根据值列表重新排序python中的字典 [英] Reorder dictionary in python according to a list of values

查看:162
本文介绍了根据值列表重新排序python中的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们考虑一个字典:

sample_dict={1:'r099',2:'g444',3:'t555',4:'f444',5:'h666'}

按照我希望的字典键的顺序列表指定的顺序排列这个字典。让我们说所需的订单列表是:

I want to re-order this dictionary in an order specified by a list containing the order of the dictionary keys that I desire. Let us say the desired order list is:

desired_order_list=[5,2,4,3,1]

所以,我希望我的字典显示如下:

So, I want my dictionary to appear like this:

{5:'h666',2:'g444',4:'f444',3:'t555',1:'r099'}

如果我可以得到一个值很好的列表。意思是,结果可以是:

If I can get a list of values that is fine too. Meaning, the result can be this:

['h666','g444','f444','t555','r099']

如何以最不复杂的方式实现?

How do I achieve this in the least complex way possible?

推荐答案

Python字典无序。

Python dictionaries are unordered.

使用 OrderedDict

这篇关于根据值列表重新排序python中的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆