如何按日期时间键对python字典进行排序?(How to sort python dictionary by datetime key?)

编程入门 行业动态 更新时间:2024-10-26 10:40:48
如何按日期时间键对python字典进行排序?(How to sort python dictionary by datetime key?)

我有一个如下的字典:

dt = {'2016-12-15 18:12:17': 1, '2016-12-15 17:40:39': 1, '2016-12-15 15:53:42': 1, '2016-12-15 15:51:41': 1, '2016-12-15 15:49:55': 1, '2016-12-15 15:49:54': 1, '2016-12-15 15:48:05': 1, '2016-12-15 15:27:38': 1, '2016-12-15 09:05:03': 1, '2016-12-15 08:43:45': 1, '2016-12-15 07:29:15': 1, '2016-12-15 05:56:58': 1, '2016-12-14 14:33:31': 1, '2016-12-14 14:33:30': 1, '2016-12-14 14:33:29': 1, '2016-12-14 14:33:28': 1, '2016-12-14 13:24:55': 1, '2016-12-14 13:15:51': 1, '2016-12-14 13:05:38': 1, '2016-12-14 12:58:47': 1}

我想在datetime键上对字典进行排序。

我试过了 :

dt = ordereddict.OrderedDict()

但它不起作用请帮忙。

I have a dictionary like below:

dt = {'2016-12-15 18:12:17': 1, '2016-12-15 17:40:39': 1, '2016-12-15 15:53:42': 1, '2016-12-15 15:51:41': 1, '2016-12-15 15:49:55': 1, '2016-12-15 15:49:54': 1, '2016-12-15 15:48:05': 1, '2016-12-15 15:27:38': 1, '2016-12-15 09:05:03': 1, '2016-12-15 08:43:45': 1, '2016-12-15 07:29:15': 1, '2016-12-15 05:56:58': 1, '2016-12-14 14:33:31': 1, '2016-12-14 14:33:30': 1, '2016-12-14 14:33:29': 1, '2016-12-14 14:33:28': 1, '2016-12-14 13:24:55': 1, '2016-12-14 13:15:51': 1, '2016-12-14 13:05:38': 1, '2016-12-14 12:58:47': 1}

I want to sort the dictionary on datetime key.

I tried :

dt = ordereddict.OrderedDict()

But it is not working please help.

最满意答案

将dt的键转换为datetime对象,例如,使用dateutil.parser.parse ,

from dateutil.parser import parse from collections import OrderedDict new_d = OrderedDict(sorted(dt.items(), key=lambda x: parse(x[0]))) print(new_d) ''' OrderedDict([ ('2016-12-14 12:58:47', 1), ('2016-12-14 13:05:38', 1), ('2016-12-14 13:15:51', 1), ('2016-12-14 13:24:55', 1), ('2016-12-14 14:33:28', 1), ('2016-12-14 14:33:29', 1), ('2016-12-14 14:33:30', 1), ('2016-12-14 14:33:31', 1), ('2016-12-15 05:56:58', 1), ('2016-12-15 07:29:15', 1), ('2016-12-15 08:43:45', 1), ('2016-12-15 09:05:03', 1), ('2016-12-15 15:27:38', 1), ('2016-12-15 15:48:05', 1), ('2016-12-15 15:49:54', 1), ('2016-12-15 15:49:55', 1), ('2016-12-15 15:51:41', 1), ('2016-12-15 15:53:42', 1), ('2016-12-15 17:40:39', 1), ('2016-12-15 18:12:17', 1)]) '''

在你的特定情况下,正如@AndreaCorbellini指出的那样,直接排序字符串工作正常,即

new_d = OrderedDict(sorted(dt.items()))

Convert the keys in dt into datetime objects, for instance, using dateutil.parser.parse,

from dateutil.parser import parse from collections import OrderedDict new_d = OrderedDict(sorted(dt.items(), key=lambda x: parse(x[0]))) print(new_d) ''' OrderedDict([ ('2016-12-14 12:58:47', 1), ('2016-12-14 13:05:38', 1), ('2016-12-14 13:15:51', 1), ('2016-12-14 13:24:55', 1), ('2016-12-14 14:33:28', 1), ('2016-12-14 14:33:29', 1), ('2016-12-14 14:33:30', 1), ('2016-12-14 14:33:31', 1), ('2016-12-15 05:56:58', 1), ('2016-12-15 07:29:15', 1), ('2016-12-15 08:43:45', 1), ('2016-12-15 09:05:03', 1), ('2016-12-15 15:27:38', 1), ('2016-12-15 15:48:05', 1), ('2016-12-15 15:49:54', 1), ('2016-12-15 15:49:55', 1), ('2016-12-15 15:51:41', 1), ('2016-12-15 15:53:42', 1), ('2016-12-15 17:40:39', 1), ('2016-12-15 18:12:17', 1)]) '''

In your specific case, as pointed by @AndreaCorbellini, sorting strings directly works fine, i.e.

new_d = OrderedDict(sorted(dt.items()))

更多推荐

本文发布于:2023-07-31 00:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340303.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字典   按日   时间   python   key

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!