2016-10-03 70 views
0

输入版本的关键排序的字典:如何通过

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]) 

谢谢

回答

0

明白了!请不要介意,发现LooseVersion /严格的版本

from distutils.version import LooseVersion 
from collections import OrderedDict 

orderedKeys = sorted(foo, key=LooseVersion) 

odict = OrderedDict((key, foo[key]) for key in orderedKeys) 

for item in odict: 
    print '{}: {}'.format(item, odict[item]) 
1

用适当的排序关键字排序foo。你必须切掉“测试”部分,然后在剩下的时间段上分割,然后将其中的每一个转换为整数。此外,结果将是一个键的列表,所以查找原始字典中的这些项目。

>>> bar = sorted(foo, key=lambda x: map(int, x.split('-')[1].split('.'))) 
>>> for item in bar: 
...  print '{}: {}'.format(item, foo[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 3你就必须包裹在map()list()tuple()消费懒惰map对象。

0

在排序功能,各执'.'的关键,并在投中的最后一项分裂列表整数

for k in sorted(foo, key=lambda x: int(x.rsplit('.')[-1])): 
    print('{}: {}'.format(k, foo[k])) 

输出:

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']