Python使用生成器generator
程序员文章站
2022-07-01 17:10:23
...
Python3,使用生成器,生成一个列表,保存从历史日期到现在所有的天
#!/usr/bin/python
import datetime
def date_print(day):
Lis = []
begin = datetime.datetime.strptime(day, '%Y-%m-%d')
#print(type(begin))
nowtime = datetime.datetime.today()
for i in range((nowtime - begin).days + 1):
yield i
date_day = begin + datetime.timedelta(days = i)
date = datetime.datetime.strftime(date_day, '%Y-%m-%d')
Lis.append(str(date))
print(Lis)
date1 = date_print("2019-11-29")
print(type(date1))
for i in date1:
pass
直接运行结果