获取年份中包含 python 中的一个字符的月份

Get the Month with year including one character in python

我正在尝试获取带有年份的月份列表,例如 [ "2019M10", "2019M11", "2019M12", "2020M01", "2020M02", "2020M03", "2020M04", "2020M05", "2020M06", "2020M07", "2020M08", "2020M09", "2020M10", "2020M11", "2020M12", "2021M01", "2021M02", "2021M03", "2021M04", 《2021M05》 ] 按照我正在使用的代码示例

import datetime
import json
from_year = 2018
last_year = datetime.datetime.now().year
print(last_year)
year_list = list(range(from_year, last_year))
new_month = []
for all_year in year_list:
    all_months = [str(all_year)+'M'+str(i) for i in list(range(1,13))]
    all_months.extend(all_months)
print(all_months )
months = json.dumps(all_months)
print(months)

我没有得到想要的输出。

使用Timestamp.to_period for actual year and month, create PeriodIndex by period_range and then convert values to format YYYYMmm by PeriodIndex.strftime:

from_year = 2018
last_year = pd.to_datetime('now').to_period('m')
print(last_year)
2021-07

months = pd.period_range(from_year, last_year, freq='M').strftime('%YM%m').tolist()
print (months)
['2018M01', '2018M02', '2018M03', '2018M04', '2018M05', '2018M06', '2018M07', '2018M08',
 '2018M09', '2018M10', '2018M11', '2018M12', '2019M01', '2019M02', '2019M03', '2019M04',
 '2019M05', '2019M06', '2019M07', '2019M08', '2019M09', '2019M10', '2019M11', '2019M12', 
 '2020M01', '2020M02', '2020M03', '2020M04', '2020M05', '2020M06', '2020M07', '2020M08', 
 '2020M09', '2020M10', '2020M11', '2020M12', '2021M01', '2021M02', '2021M03', '2021M04', 
 '2021M05', '2021M06', '2021M07']

如果需要所有月份添加下一年,然后切片 months 的最后一个值:

from_year = 2018
last_year = pd.to_datetime('now').year + 1
print(last_year)
2022

months = pd.period_range(from_year, last_year, freq='M')[:-1].strftime('%YM%m').tolist()
print (months)
['2018M01', '2018M02', '2018M03', '2018M04', '2018M05', '2018M06', '2018M07', '2018M08', 
 '2018M09', '2018M10', '2018M11', '2018M12', '2019M01', '2019M02', '2019M03', '2019M04',
 '2019M05', '2019M06', '2019M07', '2019M08', '2019M09', '2019M10', '2019M11', '2019M12',
 '2020M01', '2020M02', '2020M03', '2020M04', '2020M05', '2020M06', '2020M07', '2020M08', 
 '2020M09', '2020M10', '2020M11', '2020M12', '2021M01', '2021M02', '2021M03', '2021M04', 
 '2021M05', '2021M06', '2021M07', '2021M08', '2021M09', '2021M10', '2021M11', '2021M12']

你的嵌套列表理解的解决方案和展平:

from_year = 2018
last_year = datetime.datetime.now().year
print(last_year)
2021

year_list = list(range(from_year, last_year))

months = [f'{all_year}M{i:02}' for all_year in year_list for i in list(range(1,13))]
print (months)

['2018M01', '2018M02', '2018M03', '2018M04', '2018M05', '2018M06', '2018M07', '2018M08',
 '2018M09', '2018M10', '2018M11', '2018M12', '2019M01', '2019M02', '2019M03', '2019M04', 
 '2019M05', '2019M06', '2019M07', '2019M08', '2019M09', '2019M10', '2019M11', '2019M12', 
 '2020M01', '2020M02', '2020M03', '2020M04', '2020M05', '2020M06', '2020M07', '2020M08', 
 '2020M09', '2020M10', '2020M11', '2020M12', '2021M01', '2021M02', '2021M03', '2021M04',
 '2021M05', '2021M06', '2021M07', '2021M08', '2021M09', '2021M10', '2021M11', '2021M12']

你每次循环都在创建一个 new list 并且 extending 它。所以最后的数据被擦掉并填充了最新的数据,你正在扩展它。所以数据是出现两次。

@jezarel 给出的解决方案是最有效的,但是你可以进行这些修改

import datetime
import json
from_year = 2018
last_year = datetime.datetime.now().year
print(last_year)
year_list = list(range(from_year, last_year))
print(year_list)
new_month = []
all_months=[]
for all_year in year_list:
    new_all_months = [str(all_year)+'M'+str(i) for i in list(range(1,13))]
    all_months.extend(new_all_months)