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))
python按时间(分钟前,小时前,昨天...)排序
于 2023-06-21 11:37:46 首次发布