Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
652 views
in Technique[技术] by (71.8m points)

sorting - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...