Python:按日期对列表进行排序? [英] Python: Sort list by date?

查看:991
本文介绍了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]]

这篇关于Python:按日期对列表进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆