匿名函数+三目运算获取时间

year = str(int(time.strftime('%Y', time.localtime(time.time()))) - 2)
end_year = int(time.strftime('%Y', time.localtime(time.time())))
for year in range(int(year),end_year):
    year = str(year)
    date = [(lambda x: year + "0" + str(x) if x < 10 else year + str(x))(x) for x in range(1, 13)] 

    print(date)

输出结果如下:

['201701', '201702', '201703', '201704', '201705', '201706', '201707', '201708', '201709', '201710', '201711', '201712']
['201801', '201802', '201803', '201804', '201805', '201806', '201807', '201808', '201809', '201810', '201811', '201812']

猜你喜欢

转载自blog.csdn.net/qq_39138295/article/details/88240722