datetime模塊日期轉換和列表sorted排序

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']
相關文章
相關標籤/搜索