import datetime dt = '2019010103' # 日期 2019年1月1日3时 dts = (datetime.datetime.strptime(dt, '%Y%m%d%H') + datetime.timedelta(days=-1)).strftime('%Y%m%d%H') # 将dt向前或向后调整(day表示天,hours表示表示小时,负数往前正数往后) print(dts) # 2018123103
l1 = ['C1D02160000021700001', 'C1D02161200021700001', 'C1D02161200021703001', 'C1D02161200021706001', 'C1D02161200021709001', 'C1D02170000021712001', 'C1D02161200021712001', 'C1D02170000021715001', 'C1D02170000021718001', 'C1D02170000021721001'] # a = [[2, 3], [4, 1], (2, 8), (2, 1), (3, 4)] li = sorted(l1, key=lambda x: (x[3:9], x[11:17])) # 先按3-9位排序,相同时按11-17位排序 print(li) # ['C1D02160000021700001', 'C1D02161200021700001', 'C1D02161200021703001', 'C1D02161200021706001', # 'C1D02161200021709001', 'C1D02161200021712001', 'C1D02170000021712001', 'C1D02170000021715001', # 'C1D02170000021718001', 'C1D02170000021721001']