python怎么计算获取如“202101-202204”之间的年月份之差

python怎么计算如“202101-202204”的年月份之差

前言

今天,一个朋友问我,他想用python得到两个年月之间的所有月份的列表,比如,开始年月为20208,截止年月为202405,怎么能得到这两个年月之间,格式为[202208,202209,202210,202211,…,202204,202405]的所有年月的组合呢?

想了一起,好像不是太简单,因为月份只有12个,不能简单的用range(开始年月, 截止年月)。于是在网上搜了一圈,好像用python写的算法,还真没有很适合的。于是就当家庭作业,脱了衣服开干!(夏天还没过去,热的啊!)

大概思路

参考了几个java写的思路,自己灵光一现,感觉好像不用那么复杂,用自己的思路开始尝试:
1.把接收的“起始年月”和“截止年月”,按年和月分开,赋值给4个变量;
sta_year,sta_month,end_year, end_month
2. 定义一个12个月的列表:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
3. 定义一个存放最后需要遍历的年月的列表range_list = [];
4. 判断如果年份相同,直接把起始年月和截止年月代入range()去遍历;
5. 判断如果年份不同;
5.1. # 计算年份差,存入年份列表
year_list = [int(sta_year) + i for i in range(0, int(end_year) - int(sta_year) + 1)];
5.2. # 计算月份差,存入月份列表
month_list = month_12[int(sta_month) - 1:] + month_12[:int(end_month)];
5.4. # 计算第一年的年月的组合
year_month_first_list = [str(x) + bu_ling(y) for x in year_list[:1] for y in month_list[:-int(end_month)]]
5.5. # 计算中间N年的所有年月组合
year_month_mid_list = [str(x) + bu_ling(y) for x in year_list[1:-1] for y in month_list]
5.6. # 计算最后一个年份的年月组合
year_month_last_list = [str(x) + bu_ling(y) for x in year_list[-1:] for y in month_list[-int(end_month):]]
5.7. # 把year_month_list1和year_month_list2先去除多余重复的,再进行排序(便于检查结果是否正确),
5.7. # 再把两个列表拼接在一起,就是最后想要的年月差
range_list = sorted(list(set(year_month_first_list))) + sorted(list(set(year_month_mid_list))) + sorted(list(set(year_month_last_list)))

最终代码

# -*- coding:   utf-8 -*-
# @Time     :   2022-08-23 21:17   
# @Author   :   Kyln.Wu
# @Email    :   kylnwu@qq.com
# @FileName :   计算两个年月份的差.py
# @IDE      :   PyCharm

# time_start = input('请输入起始年月“如202201”:')
time_start = "202201"
# 取年份
sta_year = time_start[:4]
# 取月份,需要取2位,否则10,11,12月会出错
sta_month = time_start[-2:]
print(f"起始年月:{sta_year, sta_month}")
# time_end = input('请输入截止年月“如202201”:')
time_end = "202305"
# 取年份
end_year = time_end[:4]
# 取月份,需要取2位,否则10,11,12月会出错
end_month = time_end[-2:]
print(f"截止年月:{end_year, end_month}")
# 定义一个12个月的列表
month_12 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
# 定义一个存放最后需要遍历的年月的列表
range_list = []


def bu_ling(y):
    """
    :param y: y为月份,小于10月前面补0
    :return: 返回字符串:01,02,03,04...10,11,12
    """
    if y < 10:
        return "0" + str(y)
    if 9 < y < 13:
        return str(y)


# 如果年份相同,直接把起始年月和截止年月代入range()
if sta_year == end_year:
    range_list = range(int(time_start), int(time_end))
    print(range_list)

# 如果年份不同
if sta_year != end_year:
    # 计算年份差,存入年份列表
    year_list = [int(sta_year) + i for i in range(0, int(end_year) - int(sta_year) + 1)]
    # 计算月份差,存入月份列表
    month_list = month_12[int(sta_month) - 1:] + month_12[:int(end_month)]
	# 计算第一年的年月的组合
    year_month_first_list = [str(x) + bu_ling(y) for x in year_list[:1] for y in month_list[:-int(end_month)]]
    print(year_month_first_list)
    # 计算中间N年的所有年月组合
    year_month_mid_list = [str(x) + bu_ling(y) for x in year_list[1:-1] for y in month_list]
    print(year_month_mid_list)
    # 计算最后一个年份的年月组合
    year_month_last_list = [str(x) + bu_ling(y) for x in year_list[-1:] for y in month_list[-int(end_month):]]
    print(year_month_last_list)
    # 把year_month_list1和year_month_list2先去除多余重复的,再进行排序(便于检查结果是否正确),
    # 再把两个列表拼接在一起,就是最后想要的年月差
    range_list = sorted(list(set(year_month_first_list))) + sorted(list(set(year_month_mid_list))) + sorted(list(set(year_month_last_list)))
    print(range_list)

运行结果,有图有真相

python怎么计算如“202101-202204”的年月份之差

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值