我有一个清单:
a = ['7-Mar-14', '10-Mar-14', '11-Mar-14', '14-Mar-14', '15-Mar-14', '17-Mar-14', '22-Mar-14', '23-Mar-14', '25-Mar-14', '1-Nov-13', '5-Nov-13', '8-Nov-13', '23-Nov-13', '24-Nov-13', '25-Nov-13', '26-Nov-13', '3-Dec-13', '9-Dec-13', '13-Dec-13', '9-Jan-14', '17-Jan-14', '20-Jan-14', '8-Feb-14', '9-Feb-14', '10-Feb-14', '11-Feb-14', '12-Feb-14', '16-Feb-14', '17-Feb-14', '19-Feb-14', '22-Feb-14', '26-Feb-14', '28-Feb-14', '2-Mar-14', '4-Mar-14', '31-Mar-14', '1-Apr-14', '2-Apr-14', '4-Apr-14', '6-Apr-14', '8-Apr-14', '9-Apr-14', '15-Apr-14', '16-Apr-14', '17-Apr-14', '18-Apr-14', '20-Nov-13', '5-Dec-13', '15-Dec-13', '15-Jan-14', '19-Jan-14', '26-Jan-14', '3-Feb-14', '6-Feb-14', '14-Feb-14', '21-Feb-14', '24-Feb-14', '1-Mar-14', '5-Mar-14', '12-Mar-14', '19-Mar-14', '20-Mar-14', '21-Mar-14', '24-Mar-14', '26-Mar-14', '27-Mar-14', '29-Mar-14', '30-Mar-14', '7-Apr-14', '10-Apr-14', '11-Apr-14', '12-Apr-14', '27-Nov-13', '16-Jan-14', '27-Jan-14', '6-Mar-14', '13-Mar-14', '16-Mar-14', '18-Mar-14', '28-Mar-14', '3-Apr-14', '5-Apr-14']
有没有办法可以按实际日期/时间排序此列表?
答案 0 :(得分:14)
您可以使用list.sort
和datetime.datetime.strptime
:
>>> from datetime import datetime
>>> a = ['7-Mar-14', '10-Mar-14', '11-Mar-14', '14-Mar-14', '15-Mar-14', '17-Mar-14', '22-Mar-14', '23-Mar-14', '25-Mar-14', '1-Nov-13', '5-Nov-13', '8-Nov-13', '23-Nov-13', '24-Nov-13', '25-Nov-13', '26-Nov-13', '3-Dec-13', '9-Dec-13', '13-Dec-13', '9-Jan-14', '17-Jan-14', '20-Jan-14', '8-Feb-14', '9-Feb-14', '10-Feb-14', '11-Feb-14', '12-Feb-14', '16-Feb-14', '17-Feb-14', '19-Feb-14', '22-Feb-14', '26-Feb-14', '28-Feb-14', '2-Mar-14', '4-Mar-14', '31-Mar-14', '1-Apr-14', '2-Apr-14', '4-Apr-14', '6-Apr-14', '8-Apr-14', '9-Apr-14', '15-Apr-14', '16-Apr-14', '17-Apr-14', '18-Apr-14', '20-Nov-13', '5-Dec-13', '15-Dec-13', '15-Jan-14', '19-Jan-14', '26-Jan-14', '3-Feb-14', '6-Feb-14', '14-Feb-14', '21-Feb-14', '24-Feb-14', '1-Mar-14', '5-Mar-14', '12-Mar-14', '19-Mar-14', '20-Mar-14', '21-Mar-14', '24-Mar-14', '26-Mar-14', '27-Mar-14', '29-Mar-14', '30-Mar-14', '7-Apr-14', '10-Apr-14', '11-Apr-14', '12-Apr-14', '27-Nov-13', '16-Jan-14', '27-Jan-14', '6-Mar-14', '13-Mar-14', '16-Mar-14', '18-Mar-14', '28-Mar-14', '3-Apr-14', '5-Apr-14']
>>> a.sort(key=lambda date: datetime.strptime(date, "%d-%b-%y"))
>>> a
['1-Nov-13', '5-Nov-13', '8-Nov-13', '20-Nov-13', '23-Nov-13', '24-Nov-13', '25-Nov-13', '26-Nov-13', '27-Nov-13', '3-Dec-13', '5-Dec-13', '9-Dec-13', '13-Dec-13', '15-Dec-13', '9-Jan-14', '15-Jan-14', '16-Jan-14', '17-Jan-14', '19-Jan-14', '20-Jan-14', '26-Jan-14', '27-Jan-14', '3-Feb-14', '6-Feb-14', '8-Feb-14', '9-Feb-14', '10-Feb-14', '11-Feb-14', '12-Feb-14', '14-Feb-14',
'16-Feb-14', '17-Feb-14', '19-Feb-14', '21-Feb-14', '22-Feb-14', '24-Feb-14', '26-Feb-14', '28-Feb-14', '1-Mar-14', '2-Mar-14', '4-Mar-14', '5-Mar-14', '6-Mar-14', '7-Mar-14', '10-Mar-14', '11-Mar-14', '12-Mar-14', '13-Mar-14', '14-Mar-14', '15-Mar-14', '16-Mar-14', '17-Mar-14', '18-Mar-14', '19-Mar-14', '20-Mar-14', '21-Mar-14', '22-Mar-14', '23-Mar-14', '24-Mar-14', '25-Mar-14', '26-Mar-14', '27-Mar-14', '28-Mar-14', '29-Mar-14', '30-Mar-14', '31-Mar-14', '1-Apr-14', '2-Apr-14', '3-Apr-14', '4-Apr-14', '5-Apr-14', '6-Apr-14', '7-Apr-14', '8-Apr-14', '9-Apr-14',
'10-Apr-14', '11-Apr-14', '12-Apr-14', '15-Apr-14', '16-Apr-14', '17-Apr-14', '18-Apr-14']
>>>
需要strptime
函数将日期转换为datetime对象,可以进行比较。
答案 1 :(得分:0)
我的列表包含"%Y-%m-%d %H:%M:%S"
格式的“ datetime”字符串,如下所示:
from datetime import datetime
dateList = ['2018-09-07 19:00:46',
'2018-09-03 19:39:42',
'2018-09-04 09:46:42',
'2018-09-02 14:21:22',
'2018-09-05 19:19:34',
'2018-09-03 13:35:17',
'2018-09-05 08:19:35',
'2018-09-07 05:22:54',
'2018-09-07 19:01:02']
我将字符串转换为日期时间对象(格式正确),然后使用对列表理解进行操作的sorted(
)函数对它们进行排序:
sorted([datetime.strptime(dt, "%Y-%m-%d %H:%M:%S") for dt in dateList])
生成排序日期时间对象的列表,如下所示:
[datetime.datetime(2018, 9, 2, 14, 21, 22),
datetime.datetime(2018, 9, 3, 13, 35, 17),
datetime.datetime(2018, 9, 3, 19, 39, 42),
datetime.datetime(2018, 9, 4, 9, 46, 42),
datetime.datetime(2018, 9, 5, 8, 19, 35),
datetime.datetime(2018, 9, 5, 19, 19, 34),
datetime.datetime(2018, 9, 7, 5, 22, 54),
datetime.datetime(2018, 9, 7, 19, 0, 46),
datetime.datetime(2018, 9, 7, 19, 1, 2)]