I aim to develop a function that creates the list target_list, see below.
Target list has the following structure, it always starts with a date, ‘1995-01-01’, then follows eleven elements representing the following eleven month of the year. After ‘dec’ I want a new datestring, ‘1996-01-01’ and then comes the months as last year.
Target list below has length 27. I want the function to has the length of target_list as a parameter. If 28, one element would be added to target_list(‘april’).
target_list=['1995-01-01','feb', 'mar', 'april', 'maj', 'jun', 'juli', 'aug', 'sep', 'okt', 'nov', 'dec', '1996-01-01',
'feb', 'mar', 'april', 'maj', 'jun', 'juli', 'aug', 'sep', 'okt', 'nov', 'dec', '1997-01-01', 'feb', 'mar']
The code below returns the list I want. Because I want a list of 27 months I loop over 3 years. That force me to remove the last nine elements of the list. I struggle to find a way to make this process automated. Perhaps I want a list of 145 months. The function must know how many elements to remove. This I haven’t figured out. Any help or recommendations would be much appreciated. Of course more efficient solutions is also of great interest.
import math
from datetime import datetime
from datetime import date
def add_years(d, years):
try:
return d.replace(year = d.year + years)
except ValueError:
return d + (date(d.year + years, 1, 1) - date(d.year, 1, 1))
year=pd.to_datetime('1994-01-01')
years=math.ceil(27/12) #For 27 month I need three years. Use math.ceil to "round" upwards.
target=[]
for i in range(1,years+1): # Loop over the number of years
year=add_years(year,1) # Add one year for each loop
y=[year.strftime("%Y-%m-%d")]
z=y+ month # Concat the lists
target=target + z # Concat with target
target_list=target[:len(target)-9] # Remove the last nine elements of the list
print(target_list)
This is one solution.
def get_list(no_month):
import math
year=pd.to_datetime('1994-01-01')
years=math.ceil(no_month/12)
remove=(years*12)-no_month
target=[]
for i in range(1,years+1): # Loop over the number of years involved
year=add_years(year,1) # Add one year for each loop
y=[year.strftime("%Y-%m-%d")]
z=y+ month # Concat the lists
target=target + z # Concat with target
target_list=target[:len(target)-remove]
return target_list
>Solution :
I would use itertools for this task as follows
import itertools
def months():
year = itertools.count(1995)
for m in itertools.cycle(['y', 'feb', 'mar', 'april', 'maj', 'jun', 'juli', 'aug', 'sep', 'okt', 'nov', 'dec']):
yield '{}-01-01'.format(next(year)) if m=='y' else m
target145 = list(itertools.islice(months(),145))
print(target145)
output
['1995-01-01', 'feb', 'mar', 'april', 'maj', 'jun', 'juli', 'aug', 'sep', 'okt', 'nov', 'dec', '1996-01-01', 'feb', 'mar', 'april', 'maj', 'jun', 'juli', 'aug', 'sep', 'okt', 'nov', 'dec', '1997-01-01', 'feb', 'mar', 'april', 'maj', 'jun', 'juli', 'aug', 'sep', 'okt', 'nov', 'dec', '1998-01-01', 'feb', 'mar', 'april', 'maj', 'jun', 'juli', 'aug', 'sep', 'okt', 'nov', 'dec', '1999-01-01', 'feb', 'mar', 'april', 'maj', 'jun', 'juli', 'aug', 'sep', 'okt', 'nov', 'dec', '2000-01-01', 'feb', 'mar', 'april', 'maj', 'jun', 'juli', 'aug', 'sep', 'okt', 'nov', 'dec', '2001-01-01', 'feb', 'mar', 'april', 'maj', 'jun', 'juli', 'aug', 'sep', 'okt', 'nov', 'dec', '2002-01-01', 'feb', 'mar', 'april', 'maj', 'jun', 'juli', 'aug', 'sep', 'okt', 'nov', 'dec', '2003-01-01', 'feb', 'mar', 'april', 'maj', 'jun', 'juli', 'aug', 'sep', 'okt', 'nov', 'dec', '2004-01-01', 'feb', 'mar', 'april', 'maj', 'jun', 'juli', 'aug', 'sep', 'okt', 'nov', 'dec', '2005-01-01', 'feb', 'mar', 'april', 'maj', 'jun', 'juli', 'aug', 'sep', 'okt', 'nov', 'dec', '2006-01-01', 'feb', 'mar', 'april', 'maj', 'jun', 'juli', 'aug', 'sep', 'okt', 'nov', 'dec', '2007-01-01']
Explanation: firstly I create endless iterator months, internally I use itertools.cycle to cycle month codes, I used y letter to denote where year should appear, when it so I take next year starting from 1995. I use .format to get desired str holding year, otherwise month case is provided as-is. Then I get 145 first elements of endless months() and convert it into list to comply with requirements imposed.