根据值列表重新排序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