如何按版本键对字典进行排序

时间:2016-10-03 20:17:48

标签: python sorting dictionary key versions

输入:

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

理想情况下,我希望这是pythonic,因为我不会做一些疯狂的事情,比如将密钥分成组件并根据它构建新的字典;

我尝试了什么:

from collections import OrderedDict
od = OrderedDict(sorted(foo.items()))
    print '{}: {}'.format(item, od[item])

谢谢

3 个答案:

答案 0 :(得分:1)

知道了!没关系,找到了LooseVersion / strict版本

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 :(得分: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对象。

答案 2 :(得分:0)

在sort函数中,拆分'.'上的键并将 splitted 列表中的最后一项转换为整数:

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