2

我有这样的字典:

{
   '28.10.11/D/text1/' : {key:value1},
   '27.01.11/D/text2/' : {key:value2},
   '07.11.12/D/text3/' : {key:value3}
}

部分密钥基于日期,以“%d.%m.%Y”格式设置。需要按日期对所有字典键进行排序。值必须以相同的格式保存。例子:

{
   '27.01.11/D/text2/' : {key:value2},   
   '28.10.11/D/text1/' : {key:value1},
   '07.11.12/D/text3/' : {key:value3}
}
4

3 回答 3

4

您无法对标准字典进行排序,但可以对项目进行排序和显示。

from datetime import datetime

D = {'28.10.11/D/text1/' : {'key':'value1'},
     '27.01.11/D/text2/' : {'key':'value2'},
     '07.11.12/D/text3/' : {'key':'value3'}}

for k in sorted(D,key=lambda k: datetime.strptime(k[:8],"%d.%m.%y")):
    print(k,D[k])

输出:

27.01.11/D/text2/ {'key': 'value2'}
28.10.11/D/text1/ {'key': 'value1'}
07.11.12/D/text3/ {'key': 'value3'}

如果要使用列表:

from datetime import datetime
from pprint import pprint

L = [('28.10.11/D/text1/' , {'key':'value1'}),
     ('27.01.11/D/text2/' , {'key':'value2'}),
     ('07.11.12/D/text3/' , {'key':'value3'})]

L.sort(key=lambda k: datetime.strptime(k[0][:8],"%d.%m.%y"))
pprint(L)

输出:

[('27.01.11/D/text2/', {'key': 'value2'}),
 ('28.10.11/D/text1/', {'key': 'value1'}),
 ('07.11.12/D/text3/', {'key': 'value3'})]

最后,如果您仍然想要字典行为,OrderedDict 会记住插入的顺序键,因此:

from collections import OrderedDict
from datetime import datetime
from pprint import pprint

D = {'28.10.11/D/text1/' : {'key':'value1'},
     '27.01.11/D/text2/' : {'key':'value2'},
     '07.11.12/D/text3/' : {'key':'value3'}}

OD = OrderedDict(sorted(D.items(),
                 key=lambda k: datetime.strptime(k[0][:8],"%d.%m.%y"))
pprint(OD)

输出:

{'27.01.11/D/text2/': {'key': 'value2'},
 '28.10.11/D/text1/': {'key': 'value1'},
 '07.11.12/D/text3/': {'key': 'value3'}}
于 2013-03-30T16:35:46.740 回答
2

大纲

使用sortedkey参数的函数。编写key将字符串转换为 a 的函数Datetime,并根据Datetime.

代码

def remap_key(key):
    from datetime import datetime
    a = key.split('/')[0]
    return datetime.strptime(a, "%d.%m.%y")

结果

>>> d = {
...    '27.01.11/D/text2/' : "a",   
...    '28.10.11/D/text1/' : "b",
...    '07.11.12/D/text3/' : "c"
... }
>>> 
>>> def remap_key(key):
...     from datetime import datetime
...     a = key.split('/')[0]
...     return datetime.strptime(a, "%d.%m.%y")
... 
>>> sorted(d, key=remap_key)
['27.01.11/D/text2/', '28.10.11/D/text1/', '07.11.12/D/text3/']

或排序iteritems

>>> def remap_key(key_pair):
...     from datetime import datetime
...     key = key_pair[0]
...     a = key.split('/')[0]
...     return datetime.strptime(a, "%d.%m.%y")
... 
>>> sorted(d.iteritems(), key=remap_key)
[('27.01.11/D/text2/', 'a'), ('28.10.11/D/text1/', 'b'), ('07.11.12/D/text3/', 'c')]
于 2013-03-30T16:40:27.280 回答
1

作为另一种选择,您可以将OrderedDict与 datetime 结合使用(如 hughdbrown 所述):

>>> from collections import OrderedDict
>>> from datetime import datetime
>>> d = {
   '28.10.11/D/text1/' : {'key1':'value1'},
   '27.01.11/D/text2/' : {'key2':'value2'},
   '07.11.12/D/text3/' : {'key3':'value3'}
}
>>> OrderedDict(sorted(d.items(), key=lambda t: datetime.strptime(t[0][:8], "%d.%m.%y")))

The result:

OrderedDict([('27.01.11/D/text2/', {'key2': 'value2'}), ('28.10.11/D/text1/', {'key1': 'value1'}), ('07.11.12/D/text3/', {'key3': 'value3'})])

You'll get precisely the order you are looking for, and each part of the date will be compared to corresponding parts of other dates in a regular fashion.

于 2013-03-30T16:49:25.483 回答