gpt4 book ai didi

python - 创建月份列表的函数,其中 januari 是日期,其他月份是字符串

转载 作者:行者123 更新时间:2023-12-05 09:28:24 26 4
gpt4 key购买 nike

我的目标是开发一个创建列表 target_list 的函数,见下文。目标列表具有以下结构,它始终以日期“1995-01-01”开头,然后是代表一年中接下来的十一个月的十一个元素。在“dec”之后,我想要一个新的日期字符串“1996-01-01”,然后是去年的月份。

下面的目标列表长度为 27。我希望该函数将 target_list 的长度作为参数。如果为 28,则将一个元素添加到 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']

下面的代码返回我想要的列表。因为我想要一个 27 个月的列表,所以我循环了 3 年。这迫使我删除列表的最后九个元素。我很难找到一种方法来使这个过程自动化。也许我想要一份 145 个月的 list 。该函数必须知道要删除多少元素。这个我一直想不通。任何帮助或建议将不胜感激。当然,更高效的解决方案也很受关注。

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)

这是一个解决方案。

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

最佳答案

我会使用 itertools对于这个任务如下

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)

输出

['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']

解释:首先我创建了无限迭代器月,内部我使用itertools.cycle来循环月代码,我使用y字母来表示年应该出现在哪里,什么时候出现所以我从 1995 年开始选择 next 年。我使用 .format 来获得所需的 str 持有年份,否则按原样提供月份情况。然后我得到 endless months() 的 145 个第一个元素并将其转换为 list 以符合强加的要求。

关于python - 创建月份列表的函数,其中 januari 是日期,其他月份是字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71335562/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com