【发布时间】:2017-05-12 15:43:12
【问题描述】:
我有一本包含id,Direction,year,month,day,hour 的字典,我想根据month,day,hour 对其进行排序。下面是我的字典和它的值:
table = {('2339', 'W', '2016', '6', '2', '11'): [0],
('2339', 'W', '2016', '1', '16', '8'): [0],
('2339', 'W', '2016', '5', '8', '22'): [2],
('2339', 'W', '2016', '1', '17', '3'): [0]}
以及我用来排序的代码:
result_dict = sorted(table.items(), key=operator.itemgetter(0))
但这不会按照我想要的方式对其进行排序。如何根据月、日和小时对其进行排序。我想要的结果是这样的:
ID Direction year month day hour
2339 W 2016 1 1 0
2339 W 2016 1 1 1
2339 W 2016 1 1 2
2339 W 2016 1 1 3
... .. .. .. .. ..
【问题讨论】:
-
按月、日、小时排序是什么意思?从最新到最旧排序?
-
result_dict的预期结构是什么?与原始的相同,但已排序? -
另外,Python 中的字典本质上是无序的,因此您的结果将是某种其他类型的数据结构。你想要什么样的结果?
-
@errata 是的与表相同的结构。
标签: python sorting dictionary