使用:
chi_sort = lambda item: your_list_here.index(item[0])
(Replace your_list_here
with their list)
例( 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 )]