我想使用 for 循环获取开始日期和结束日期之间范围的唯一 yyyy-mm-01 列表。这是我的代码:
import datetime
from datetime import timedelta, date
start=date(2017,4,1)
end=(datetime.date.today().replace(day=1)) - datetime.timedelta(days=1) #last completed month
def daterange(start, end):
for n in range(int((end - start).days)):
yield start + timedelta(n)
for single_date in daterange(start, end):
myset = str(single_date.strftime("%Y-%m-01"))
print(myset)
The issue is that my result looks like this:
2017-04-01
2017-04-01
2017-04-01
2017-04-01
2017-04-01
2017-04-01
2017-04-01
2017-04-01
2017-04-01
2017-04-01
2017-04-01
2017-04-01
2017-04-01
2017-04-01
2017-04-01
2017-04-01
2017-04-01
2017-04-01
2017-04-01
2017-04-01
2017-04-01
2017-04-01
2017-04-01
2017-04-01
2017-05-01
我尝试添加 set() 但它仍然显示冗余日期。我该如何解决?
12345678_0001
梦里花落0921
牧羊人nacy
慕斯709654
相关分类