时间排序python,在python中按月排序日期列表

I have a list like this:

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

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.

Any will be really appreciated

解决方案

Try sorted with key.

from datetime import datetime

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值