在python中按月对日期列表进行排序 [英] Sort a list of dates by month in python

查看:650
本文介绍了在python中按月对日期列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的列表:

['25/May/2015', '27/May/2015', '27/Apr/2015', '27/Jan/2015', '07/May/2015' '22/May/2015', '16/Jan/2015', '29/Jan/2015', '28/Feb/2015', '18/Feb/2015', '08/May/2015', '20/Jan/2015', '24/Jan/2015', '31/Mar/2015', '30/Apr/2015', '17/Feb/2015', '19/Mar/2015', '05/May/2015', '22/Jan/2015', '14/Aug/2015', '26/Feb/2015', '14/Mar/2015', '28/May/2015']']

我想按月份订购日期:1月,2月,3月等. 但我也想按天订购,这意味着第一天必须是:1/jan/2015然后2/jan/2015

I want to order the dates by month: jan, feb, march, etc... but also i want to order them by day, this means the first day must be: 1/jan/2015 then 2/jan/2015

我试图对列表进行排序:

i tried to sort the list:

days.sort()

但是按天而不是按月排序列表.

BUT it´s ordering the list by days and not by months.

任何人都会非常感激

推荐答案

尝试已排序和键.

from datetime import datetime
days_sorted = sorted(days, key=lambda day: datetime.strptime(day, "%d/%b/%Y"))

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

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