Python:在两个日期之间生成YYMM字符串

Python:在两个日期之间生成YYMM字符串,python,Python,我有开始年、结束年、开始月和结束月,例如 start_year = 2005 end_year = 2017 start_month = 3 end_month = 2 我想生成以下字符串: 0503.mat,0504.mat,0505.mat,0506.mat,0507.mat,0508.mat,0509.mat,0510.mat,0511.mat,0512.mat,0601.mat,…0612.mat,…1701.mat,1702.mat i、 e.合并年和月,并为给定开始年/月和

我有开始年、结束年、开始月和结束月,例如

start_year = 2005
end_year   = 2017
start_month = 3
end_month   = 2
我想生成以下字符串: 0503.mat,0504.mat,0505.mat,0506.mat,0507.mat,0508.mat,0509.mat,0510.mat,0511.mat,0512.mat,0601.mat,…0612.mat,…1701.mat,1702.mat

i、 e.合并年和月,并为给定开始年/月和结束年/月之间的所有月份生成此类组合

编辑:

.mat在所有输出上重复

这就是我迄今为止所尝试的:

k = 0
for yr in range( 2005, 2007 + 1 ):
    for mn in range( 1, 12 + 1 ):
        YYMM[k]    =   "{:02}{:02}.mat".format(yr % 100, mn) )
        k = k + 1

但是很明显,如果我从2005年的第3个月开始,到2006年的第7个月,它不会覆盖所有的月份。这可能会更快,并且可能更容易阅读,但是让数学来完成这项工作:

strings = ["{}{}.mat".format(str(year - 2000).zfill(2), str(month).zfill(2)) \
           for year in range(start_year, end_year + 1) \
           for month in range(start_month, end_month + 1)]
>>> start = (start_year - 2000) * 12 + start_month
>>> end = (end_year - 2000) * 12 + end_month
>>> ["{:02d}{:02d}.mat".format(x // 12, x % 12) for x in range(start, end+1)]
缩写输出

['0503.mat', '0504.mat', '0505.mat', '0506.mat', ...
'1610.mat', '1611.mat', '1700.mat', '1701.mat', '1702.mat']

这可以更快,也可能更具可读性,但让数学来完成这项工作:

>>> start = (start_year - 2000) * 12 + start_month
>>> end = (end_year - 2000) * 12 + end_month
>>> ["{:02d}{:02d}.mat".format(x // 12, x % 12) for x in range(start, end+1)]
缩写输出

['0503.mat', '0504.mat', '0505.mat', '0506.mat', ...
'1610.mat', '1611.mat', '1700.mat', '1701.mat', '1702.mat']
非常简单:

start_year = 2005
end_year = 2007
start_month = 3
end_month = 2
yymm = [(yy, mm) for yy in range(start_year, end_year + 1) for mm in range(1, 13)
        if (start_year, start_month) <= (yy, mm) <= (end_year, end_month)]
我将把字符串格式留给您

工作原理:我们利用Python的元组比较机制。比较
(x1,x2,…xn)
(y1,y2,…yn)
时,顺序由
x1
y1
确定,如果它们不相等,则由
x2
y2
确定。。。等等

编辑: 要生成字符串,还需要使用list comp:forloop:

formatted_yymm = ['{:>02}{:>02}.mat'.format(yy % 100, mm) for yy, mm in yymm]
结果:

['0503.mat', '0504.mat', '0505.mat', '0506.mat', '0507.mat', '0508.mat',
 '0509.mat', '0510.mat', '0511.mat', '0512.mat', '0601.mat', '0602.mat',
 '0603.mat', '0604.mat', '0605.mat', '0606.mat', '0607.mat', '0608.mat',
 '0609.mat', '0610.mat', '0611.mat', '0612.mat', '0701.mat', '0702.mat']
非常简单:

start_year = 2005
end_year = 2007
start_month = 3
end_month = 2
yymm = [(yy, mm) for yy in range(start_year, end_year + 1) for mm in range(1, 13)
        if (start_year, start_month) <= (yy, mm) <= (end_year, end_month)]
我将把字符串格式留给您

工作原理:我们利用Python的元组比较机制。比较
(x1,x2,…xn)
(y1,y2,…yn)
时,顺序由
x1
y1
确定,如果它们不相等,则由
x2
y2
确定。。。等等

编辑: 要生成字符串,还需要使用list comp:forloop:

formatted_yymm = ['{:>02}{:>02}.mat'.format(yy % 100, mm) for yy, mm in yymm]
结果:

['0503.mat', '0504.mat', '0505.mat', '0506.mat', '0507.mat', '0508.mat',
 '0509.mat', '0510.mat', '0511.mat', '0512.mat', '0601.mat', '0602.mat',
 '0603.mat', '0604.mat', '0605.mat', '0606.mat', '0607.mat', '0608.mat',
 '0609.mat', '0610.mat', '0611.mat', '0612.mat', '0701.mat', '0702.mat']


.mat
在所有输出上都重复?是的。mat在所有输出上都重复。那么,到目前为止,您尝试了什么?我不知道从何处开始高效地执行此操作。我曾考虑过使用for循环,但它在我的情况下不起作用。这很好…但至少在尝试后…这样你的问题就不会得到太多的反对票并被标记为结束…
。mat
在所有输出上重复?是的。mat在所有输出上重复…那么你到目前为止尝试了什么?我不知道从哪里开始做效率高。我曾考虑过使用for循环,但它在我的情况下不起作用。这很好…但至少在尝试之后…所以你的问题不会得到太多的反对票并被标记为结束…抱歉,我的问题不清楚。上述解决方案中的月份不包括所有月份。月份范围为1-12个周期。我不清楚我的问题。上述解决方案中的月份并不涵盖所有月份。月份范围为1-12个周期。您是否介意在此处发布用于组合字符串的代码,因为我再次考虑编写一个for循环来组合元组元素。感谢您,我从未猜到您创建的字符串操作代码。您是否介意发布用于组合字符串的代码这里的字符串,我再次考虑编写一个for循环来组合元组的元素。谢天谢地,我从来没有猜到您创建的字符串操作代码。