第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

為日期范圍內(nèi)的每個(gè)月生成(StartDateOfMonth,EndDateofMonth)

為日期范圍內(nèi)的每個(gè)月生成(StartDateOfMonth,EndDateofMonth)

開(kāi)滿天機(jī) 2021-03-15 17:14:11
我想為指定的日期范圍生成(StartDateOfMonth,EndDateOfMonth)值的列表。例如,時(shí)間范圍:2011-09-11、2013-04-24,列表應(yīng)為:[('2011-9-11', '2011-9-30'), ('2011-10-01', '2011-10-31'), ('2011-11-01', '2011-11-30'), ('2011-12-01', '2011-12-31'), ('2012-1-01', '2012-1-31'), ('2012-2-01', '2012-2-29'), ('2012-3-01', '2012-3-31'), ('2012-4-01', '2012-4-30'), ('2012-5-01', '2012-5-31'), ('2012-6-01', '2012-6-30'), ('2012-7-01', '2012-7-31'), ('2012-8-01', '2012-8-31'), ('2012-9-01', '2012-9-30'), ('2012-10-01', '2012-10-31'), ('2012-11-01', '2012-11-30'), ('2012-12-01', '2012-12-31'), ('2013-1-01', '2013-1-31'), ('2013-2-01', '2013-2-28'), ('2013-3-01', '2013-3-31'), ('2013-4-01', '2013-4-24')]我想出了一個(gè)看起來(lái)很難看的代碼。部分原因是由于我缺乏列表一致性和Python的其他功能。代碼是:def getMonthRanges(startDate, endDate):    dateRange = []    allYears= [eachYear for eachYear in range(startDate.year, endDate.year+1)]    allMonths= [eachMonth for eachMonth in range(1, 13)]    for eachYear in allYears:        for eachMonth in allMonths:            if eachYear == startDate.year:                if eachMonth == startDate.month:                   startOfMonth = str(eachYear)+'-'+str(eachMonth) + '-'+str(startDate.day)                   endOfMonth =   str(eachYear)+ '-'+str(eachMonth) + '-'+str(calendar.monthrange(eachYear, eachMonth)[1])                   dateRange.append((startOfMonth, endOfMonth))                elif eachMonth > startDate.month:                   startOfMonth = str(eachYear)+ '-'+str(eachMonth) + '-01'                   endOfMonth = str(eachYear)+'-'+str(eachMonth)+ '-'+ str(calendar.monthrange(eachYear, eachMonth)[1])                   dateRange.append((startOfMonth, endOfMonth))    return dateRange是否要求其他開(kāi)發(fā)者反饋該代碼是否可以壓縮/改進(jìn)?
查看完整描述

2 回答

?
撒科打諢

TA貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個(gè)贊

這是僅使用datetime模塊來(lái)完成此操作的一種方法:


>>> from datetime import date, timedelta

>>> from pprint import pprint


>>> def next_month(x):

        'Advance the first of the month, wrapping the year if necessary'

        if x.month < 12:

            return x.replace(month=x.month+1, day=1)

        return x.replace(year=x.year+1, month=1)


>>> def getMonthRanges(startDate, endDate):

        result = []

        first = startDate

        while first < endDate:

            nm = next_month(first)

            last = min(endDate, nm - timedelta(days=1))

            result.append([str(first), str(last)])

            first = nm

        return result


>>> pprint(getMonthRanges(date(2011, 9, 11), date(2013, 4, 24)))

[['2011-09-11', '2011-09-30'],

 ['2011-10-01', '2011-10-31'],

 ['2011-11-01', '2011-11-30'],

 ['2011-12-01', '2011-12-31'],

 ['2012-01-01', '2012-01-31'],

 ['2012-02-01', '2012-02-29'],

 ['2012-03-01', '2012-03-31'],

 ['2012-04-01', '2012-04-30'],

 ['2012-05-01', '2012-05-31'],

 ['2012-06-01', '2012-06-30'],

 ['2012-07-01', '2012-07-31'],

 ['2012-08-01', '2012-08-31'],

 ['2012-09-01', '2012-09-30'],

 ['2012-10-01', '2012-10-31'],

 ['2012-11-01', '2012-11-30'],

 ['2012-12-01', '2012-12-31'],

 ['2013-01-01', '2013-01-31'],

 ['2013-02-01', '2013-02-28'],

 ['2013-03-01', '2013-03-31'],

 ['2013-04-01', '2013-04-24']]


查看完整回答
反對(duì) 回復(fù) 2021-03-23
  • 2 回答
  • 0 關(guān)注
  • 298 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)