我有一个字典,如下所示:
original_dict = {'two':'2','three':'3','one':'1','foo':'squirrel'}
我想让它按照这个顺序显示:
ordered_dict = OrderedDict({'one':'1','two':'2','three':'3','foo':'squirrel'
})
但是我没有得到相同的顺序,{'one':'1','two':'2','three':'3','foo':'squirrel'}
会自己创建一个字典,所以它不能按照我期望的那样运行。
我在文档中看到 sorted 方法可以使用。
OrderedDict(sorted(d.items(), key=lambda t: len(t[0])))
但我不知道有什么函数可以返回我想要的顺序 我尝试过
ordered_dict = OrderedDict(sorted(original_dict.items(),['one','two','three','foo']))
但那并没有起作用。
请注意,我想要的顺序可能非常任意,例如:
['three','foo','one','two',]