Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何按版本的键对dict进行排序_Python_Sorting_Dictionary_Key_Versions - Fatal编程技术网

Python 如何按版本的键对dict进行排序

Python 如何按版本的键对dict进行排序,python,sorting,dictionary,key,versions,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'], 'tes

输入:

整理一下

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)
理想情况下,我希望这是一个pythonic,因为我不会做一些疯狂的事情,比如将键拆分为组件,并在此基础上构建一个新的字典

我尝试的是:

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

谢谢

明白了!nevermind,找到松散版本/严格版本

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

使用适当的排序键排序
foo
。你必须切掉“测试”部分,然后将其余部分按周期分割,然后将每个周期转换为一个整数。此外,结果将是一个键列表,因此请在原始字典中查找这些项

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

请注意,对于Python 3,您必须将
map()
包装在
list()
tuple()
中才能使用惰性
map
对象。

在排序函数中,拆分
上的键。
并将拆分列表中的最后一项强制转换为整数:

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

输出:

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