用这个:
chi_sort = lambda item: your_list_here.index(item[0])
(替换your_list_here
为您的列表)
示例(collections.OrderedDict
可以用普通替换dict
):
>>> import collections
>>> ordereddict = collections.OrderedDict((
... ('key_78', 'value'),
... ('key_40', 'value'),
... ('key_96', 'value'),
... ('key_53', 'value'),
... ('key_04', 'value'),
... ('key_89', 'value'),
... ('key_52', 'value'),
... ('key_86', 'value'),
... ('key_16', 'value'),
... ('key_63', 'value'),
... ))
>>>
>>> alist = sorted(ordereddict.keys())
>>> alist
['key_04', 'key_16', 'key_40', 'key_52', 'key_53', 'key_63', 'key_78', 'key_86',
'key_89', 'key_96']
>>> sorted(ordereddict.items(), key=lambda item: alist.index(item[0]))
[('key_04', 'value'), ('key_16', 'value'), ('key_40', 'value'), ('key_52', 'valu
e'), ('key_53', 'value'), ('key_63', 'value'), ('key_78', 'value'), ('key_86', '
value'), ('key_89', 'value'), ('key_96', 'value')]