Python:按日期对列表进行排序?

Python:按日期对列表进行排序?

问题描述:

是否可以按日期对列表进行排序?它用于浮点图,因此必须以较小列表对列表的这种格式进行组织.我希望能够按日期对其进行排序.

Is it possible to sort this list by date? It is for a flot graph so it has to be organized in this format of a list of smaller list pairs. I would like to be able to sort it by date.

[["2014-5-29", 19], ["2014-5-28", 16], ["2014-5-30", 20], ["2014-5-23", 16], ["2014-5-22", 1225], ["2014-5-21", 114], ["2014-5-20", 69], ["2014-5-27", 10], ["2014-5-31", 17], ["2014-5-25", 18], ["2014-5-24", 19], ["2014-5-26", 18], ["2014-6-2", 19], ["2014-6-1", 19], ["2014-5-18", 4], ["2014-5-19", 27]]

谢谢!

这是一种方法:

In [9]: l = [["2014-5-29", 19], ["2014-5-28", 16], ["2014-5-30", 20], ["2014-5-23", 16], ["2014-5-22", 1225], ["2014-5-21", 114], ["2014-5-20", 69], ["2014-5-27", 10], ["2014-5-31", 17], ["2014-5-25", 18], ["2014-5-24", 19], ["2014-5-26", 18], ["2014-6-2", 19], ["2014-6-1", 19], ["2014-5-18", 4], ["2014-5-19", 27]]

In [10]: sorted(l, key=lambda (date, _): map(int, date.split('-')))
Out[10]: 
[['2014-5-18', 4],
 ['2014-5-19', 27],
 ['2014-5-20', 69],
 ['2014-5-21', 114],
 ['2014-5-22', 1225],
 ['2014-5-23', 16],
 ['2014-5-24', 19],
 ['2014-5-25', 18],
 ['2014-5-26', 18],
 ['2014-5-27', 10],
 ['2014-5-28', 16],
 ['2014-5-29', 19],
 ['2014-5-30', 20],
 ['2014-5-31', 17],
 ['2014-6-1', 19],
 ['2014-6-2', 19]]