发电机
这里是你如何创建一个可生成一个包含范围的函数:
def inc_range(stop, start=0, step=1):
numerator = start
while numerator<=stop:
yield numerator
numerator += step
a = [val for val in inc_range(10)]
print(a)
显示:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
现在,这个方法可以适用于任何你想要的;日期或其他。
日期范围生成
这适合您需要的方法:
from datetime import date, timedelta
def date_range(stop, start, step=1):
delta_date = stop - start
numerator = 0
while numerator <= delta_date.days:
yield str(start + timedelta(numerator))
numerator += step
date_start = date(2014, 3, 15)
date_end = date(2014, 3, 21)
a = [val for val in date_range(date_end, date_start)]
print(a)
显示:
['2014-03-15', '2014-03-16', '2014-03-17', '2014-03-18', '2014-03-19', '2014-03-20', '2014-03-21']
注意的是,在发电机产生的时候,如果你不包括str()
,该响应将代替datetime.date()
的对象。
希望这回答你的问题。
我不知道我明白你想要什么。你可以添加一个你正在寻找的值的例子吗?第二次约会,为什么'00:00'是排他性的? –
如果你正在使用日期,为什么不使用'datetime.date()'对象? –
更确切地说,'day'应该具有什么样的价值,以及你期待从'get_epoch'得到的返回值。 –