输入版本的关键排序的字典:如何通过
foo = {
'testing-1.30.5': ['The', 'quick'],
'testing-1.30.12': ['fox', 'jumped', 'over'],
'testing-1.30.13': ['the'],
'testing-1.30.4': ['lazy', 'dog'],
'testing-1.30.1': ['brown'],
'testing-1.30.3': ['the'],
'testing-1.30.6': ['brown'],
'testing-1.30.2': ['fox', 'jumped', 'over'],
'testing-1.30.14': ['lazy', 'dog'],
'testing-1.30.8': ['the'],
'testing-1.30.0': ['The', 'quick'],
'testing-1.30.10': ['The', 'quick'],
'testing-1.30.11': ['brown'],
'testing-1.30.7': ['fox', 'jumped', 'over'],
'testing-1.30.9': ['lazy', 'dog']
}
做一些整理
bar = sortfoo(foo)
所需的输出:
for item in bar:
print '{}: {}'.format(item, bar[item])
testing-1.30.0: ['The', 'quick']
testing-1.30.1: ['brown']
testing-1.30.2: ['fox', 'jumped', 'over']
testing-1.30.3: ['the']
testing-1.30.4: ['lazy', 'dog']
testing-1.30.5: ['The', 'quick']
testing-1.30.6: ['brown']
testing-1.30.7: ['fox', 'jumped', 'over']
testing-1.30.8: ['the']
testing-1.30.9: ['lazy', 'dog']
testing-1.30.10: ['The', 'quick']
testing-1.30.11: ['brown']
testing-1.30.12: ['fox', 'jumped', 'over']
testing-1.30.13: ['the']
testing-1.30.14: ['lazy', 'dog']
理想情况下,我想这是Python的,因为我不会做一些疯狂的事情,比如将密钥分解成组件并根据它创建一个新的字典;
我试了一下:
from collections import OrderedDict
od = OrderedDict(sorted(foo.items()))
print '{}: {}'.format(item, od[item])
谢谢