【发布时间】:2017-05-26 22:12:17
【问题描述】:
假设我的期间是NOV-2016 和MAR-2018,我需要打印介于(NOV-2016, DEC-2016, JAN-2017 到MAR-2018) 之间的所有期间。可以做些什么来获得想要的结果。现在我这样做了,但我没有得到想要的结果:
start_period = 'NOV-2017'
end_period = 'JUN-2019'
array = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
year1 = int(start_period.split('-')[1])
year2 = int(end_period.split('-')[1])
diff = year2-year1
month_start = start_period.split('-')[0]
month_end = end_period.split('-')[0]
index1 = array.index(month_start)
index2 = array.index(month_end)
while diff>0:
while(diff>=1 and (index2+1) != index1):
if(index1==12):
index1 = 0
print(array[index1])
index1+=1
diff-=1
if diff==0:
break
【问题讨论】:
-
你得到的结果是什么?
-
11 月 12 月 1 月 2 月 3 月 4 月 5 月 6 月
-
但我还应该从 2018 年和 2019 年从 7 月得到另一个输出到 6 月
-
将
diff>0更改为diff>=0。因为您还想处理期间为同一年的情况。
标签: python