I have range of dates like "2017-05-01", "2017-05-18"
. So, I want to split this range by daily intervals and get such result: [["2017-05-01","2017-05-02"], ["2017-05-3","2017-05-04"], ....]
Firstly, decompose this into two steps:
For the first step, there is an answer here: Iterating through a range of dates in Python in which the following generator is defined:
from datetime import timedelta, date
def daterange(start_date, end_date):
for n in range(int ((end_date - start_date).days)):
yield start_date + timedelta(n)
If we then pair those using this definition from the itertools documentation:
def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)
So then we can compose those together to make the required generator:
daypairs = grouper(daterange(start_date, end_date), 2)
And listify it thus:
daypairslist = list(daypairs)
etc.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments