如何解析OrderedDict中的Value然后排序?
How to parse Value in OrderedDict and then sort it?
我有一个多维 OrderedDict,键“stampdate”的值是一个字符串,我必须以日期时间格式解析它,然后我想对它进行排序。
########################################### ################
这是我当前的 Ordered Dict:
[
OrderedDict(
[
("id", 56),
("werkzeug", 1006),
("stampdate", "2021-12-12T10:48:19.735532"),
("aktivstatus", "1"),
]
),
OrderedDict(
[
("id", 57),
("werkzeug", 1006),
("stampdate", "2021-12-15T10:49:33.876611"),
("aktivstatus", "1"),
]
),
OrderedDict(
[
("id", 60),
("werkzeug", 1006),
("stampdate", "2021-12-12T13:42:46.216805"),
("aktivstatus", "1"),
]
),
OrderedDict(
[
("id", 230),
("werkzeug", 1006),
("stampdate", "2022-01-11T21:40:39.489662"),
("aktivstatus", "1"),
]
),
]
结果应如下所示:
[
OrderedDict(
[
("id", 56),
("werkzeug", 1006),
("stampdate", "2021-12-12T10:48:19.735532"),
("aktivstatus", "1"),
]
),
OrderedDict(
[
("id", 60),
("werkzeug", 1006),
("stampdate", "2021-12-12T13:42:46.216805"),
("aktivstatus", "1"),
]
),
OrderedDict(
[
("id", 57),
("werkzeug", 1006),
("stampdate", "2021-12-15T10:49:33.876611"),
("aktivstatus", "1"),
]
),
OrderedDict(
[
("id", 230),
("werkzeug", 1006),
("stampdate", "2022-01-11T21:40:39.489662"),
("aktivstatus", "1"),
]
),
]
>>> from datetime import datetime
>>> sorted(list_of_odered_dicts,key=lambda x: datetime.strptime(x['stampdate'], '%Y-%m-%dT%H:%M:%S.%f'))
[OrderedDict([('id', 56), ('werkzeug', 1006), ('stampdate', '2021-12-12T10:48:19.735532'), ('aktivstatus', '1')]), OrderedDict([('id', 60), ('werkzeug', 1006), ('stampdate', '2021-12-12T13:42:46.216805'), ('aktivstatus', '1')]), OrderedDict([('id', 57), ('werkzeug', 1006), ('stampdate', '2021-12-15T10:49:33.876611'), ('aktivstatus', '1')]), OrderedDict([('id', 230), ('werkzeug', 1006), ('stampdate', '2022-01-11T21:40:39.489662'), ('aktivstatus', '1')])]
我有一个多维 OrderedDict,键“stampdate”的值是一个字符串,我必须以日期时间格式解析它,然后我想对它进行排序。
########################################### ################
这是我当前的 Ordered Dict:
[
OrderedDict(
[
("id", 56),
("werkzeug", 1006),
("stampdate", "2021-12-12T10:48:19.735532"),
("aktivstatus", "1"),
]
),
OrderedDict(
[
("id", 57),
("werkzeug", 1006),
("stampdate", "2021-12-15T10:49:33.876611"),
("aktivstatus", "1"),
]
),
OrderedDict(
[
("id", 60),
("werkzeug", 1006),
("stampdate", "2021-12-12T13:42:46.216805"),
("aktivstatus", "1"),
]
),
OrderedDict(
[
("id", 230),
("werkzeug", 1006),
("stampdate", "2022-01-11T21:40:39.489662"),
("aktivstatus", "1"),
]
),
]
结果应如下所示:
[
OrderedDict(
[
("id", 56),
("werkzeug", 1006),
("stampdate", "2021-12-12T10:48:19.735532"),
("aktivstatus", "1"),
]
),
OrderedDict(
[
("id", 60),
("werkzeug", 1006),
("stampdate", "2021-12-12T13:42:46.216805"),
("aktivstatus", "1"),
]
),
OrderedDict(
[
("id", 57),
("werkzeug", 1006),
("stampdate", "2021-12-15T10:49:33.876611"),
("aktivstatus", "1"),
]
),
OrderedDict(
[
("id", 230),
("werkzeug", 1006),
("stampdate", "2022-01-11T21:40:39.489662"),
("aktivstatus", "1"),
]
),
]
>>> from datetime import datetime
>>> sorted(list_of_odered_dicts,key=lambda x: datetime.strptime(x['stampdate'], '%Y-%m-%dT%H:%M:%S.%f'))
[OrderedDict([('id', 56), ('werkzeug', 1006), ('stampdate', '2021-12-12T10:48:19.735532'), ('aktivstatus', '1')]), OrderedDict([('id', 60), ('werkzeug', 1006), ('stampdate', '2021-12-12T13:42:46.216805'), ('aktivstatus', '1')]), OrderedDict([('id', 57), ('werkzeug', 1006), ('stampdate', '2021-12-15T10:49:33.876611'), ('aktivstatus', '1')]), OrderedDict([('id', 230), ('werkzeug', 1006), ('stampdate', '2022-01-11T21:40:39.489662'), ('aktivstatus', '1')])]