python按时间(分钟前,小时前,昨天...)排序

在这里插入图片描述

times=['5小时前','6小时前','2小时前','5分钟前','昨天22:14','1小时前','昨天21:11','昨天22:05']
print('替换前:',times)
def time_sort(times):
    for t in times:
        count=0
        for time in times:
            times[count] = time
            if count >0:
                if '分钟' in time:
                    if '小时' in times[count-1]:
                            times[count]=times[count-1]
                            times[count-1]=time
                if '分钟' in time:
                    if '昨天' in times[count-1]:
                            times[count]=times[count-1]
                            times[count-1]=time
                if '小时' in time:
                    if '昨天' in times[count-1]:
                            times[count]=times[count-1]
                            times[count-1]=time
                if '小时' in time:
                    if '小时' in times[count-1]:
                            h1 = time.replace('小时前','')
                            h2 = times[count-1].replace('小时前','')
                            if int(h2)>int(h1):
                                times[count]=times[count-1]
                                times[count-1]=time
                if '分钟' in time:
                    if '分钟' in times[count-1]:
                            h1 = time.replace('分钟前','')
                            h2 = times[count-1].replace('分钟前','')
                            if int(h2)>int(h1):
                                times[count]=times[count-1]
                                times[count-1]=time
                if '昨天' in time:
                    if '昨天' in times[count-1]:
                            h1 = time.replace('昨天','').split(':')[0]
                            h2 = times[count-1].replace('昨天','').split(':')[0]
                            m1 = time.replace('昨天','').split(':')[1]
                            m2 = times[count-1].replace('昨天','').split(':')[1]
                            if int(h2)<int(h1):
                                times[count]=times[count-1]
                                times[count-1]=time
                            if int(h2)==int(h1):
                                 if int(m2)<int(m1):                                    
                                    times[count]=times[count-1]
                                    times[count-1]=time 
            count+=1
    return times

print('替换后:',time_sort(times))


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值