简体   繁体   中英

Sort a Python date string list

I have a list:

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']

Is there a way I could be able to sort this list, order by actual date/time?

You can use list.sort and 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']
>>>

The strptime function is needed to convert the dates into datetime objects, which can be compared.

my list contained 'datetime' strings in "%Y-%m-%d %H:%M:%S" format, as follows:

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']

I converted the strings to datetime objects (in the correct format) and then sorted them using the sorted( ) function operating on a list comprehension:

sorted([datetime.strptime(dt, "%Y-%m-%d %H:%M:%S") for dt in dateList])

to produce a list of sorted datetime objects as follows:

[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)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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