简体   繁体   English

按日期排序列表的python字典

[英]Sorting a python dictionary of lists by date

I'm writing a Django project as a student intern. 我正在写一个Django项目作为学生实习生。 I have the following dictionary of list of data in my python file : 我的python文件中有以下数据列表字典:

list_sequences = {'01': [["Calcul Red Shift", "2014-09-24", "Running"]],
                  '02': [["Calcul Dérive", "2014-09-22", "Stopped"]],
                  '03': [["Calcul simple", "2014-04-12", "Paused"]],
                  '04': [["Calcul Mental", "2014-02-14", "Failed"]],
                  '05': [["Sauvegarde des données", "2014-12-22", "Running"]],
                  '06': [["Envoi des données", "2014-07-17", "Stopped"]],
                  '07': [["Calcul de LA question", "2015-01-06", "Running"]],
                  '08': [["Refactorisation", "2014-09-24", "Paused"]],
                  '09': [["Cuisson de choucroute", "2013-11-13", "Running"]],
                  '10': [["Ecriture du rapport", "2014-02-03", "Running"]],
                  '11': [["Nettoyage des fichiers", "2014-09-24", "Paused"]],
                  '12': [["Création des utilisateurs", "2015-01-06", "Failed"]]
}

As you can see, the key of each element is 01, 02, ... 如您所见,每个元素的关键是01,02,...

I also use double [[ ]] because I use strings and otherwise the webpage shows only one character in the table cells. 我也使用double [[]],因为我使用字符串,否则网页只显示表格单元格中的一个字符。

What I would like to do is sorting the whole dictionary by date. 我想做的是按日期排序整个字典。

I tried the following : 我尝试了以下方法:

list_sequences = collections.OrderedDict(sorted(list_sequences.items(), key=lambda e: e[0][1]))

and also this : 还有这个:

list_sequences = collections.OrderedDict(sorted(list_sequences.items(), key=lambda e: datetime.datetime.strptime(e[0][1], '%Y-%m-%d')))

But it doesn't seem to work. 但它似乎没有用。 The first one sorts my list in a weird way and the second one throws me a : 第一个以奇怪的方式对我的列表进行排序,第二个给我一个:

time data '0' does not match format '%Y-%m-%d'

Any idea ? 任何的想法 ?

Thanks a lot ! 非常感谢 !

dict.items() returns (key, value) pairs, so the value is at index 1( e[1] ) not 0( e[0] ). dict.items()返回(键,值)对,因此值在索引1( e[1] )而不是0( e[0] )。 You need to change e[0][1] to e[1][0][1] . 您需要将e[0][1]更改为e[1][0][1]

Demo: 演示:

>>> OrderedDict(sorted(list_sequences.items(), key=lambda e:datetime.strptime(e[1][0][1], '%Y-%m-%d')))
OrderedDict([('09', [['Cuisson de choucroute', '2013-11-13', 'Running']]), ('10', [['Ecriture du rapport', '2014-02-03', 'Running']]), ('04', [['Calcul Mental', '2014-02-14', 'Failed']]), ('03', [['Calcul simple', '2014-04-12', 'Paused']]), ('06', [['Envoi des donn\xc3\xa9es', '2014-07-17', 'Stopped']]), ('02', [['Calcul D\xc3\xa9rive', '2014-09-22', 'Stopped']]), ('11', [['Nettoyage des fichiers', '2014-09-24', 'Paused']]), ('01', [['Calcul Red Shift', '2014-09-24', 'Running']]), ('08', [['Refactorisation', '2014-09-24', 'Paused']]), ('05', [['Sauvegarde des donn\xc3\xa9es', '2014-12-22', 'Running']]), ('12', [['Cr\xc3\xa9ation des utilisateurs', '2015-01-06', 'Failed']]), ('07', [['Calcul de LA question', '2015-01-06', 'Running']])])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM