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

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

帶有日期時(shí)間對(duì)象的列表的最后一個(gè)元素

帶有日期時(shí)間對(duì)象的列表的最后一個(gè)元素

尚方寶劍之說 2023-08-08 16:48:01
我有 2 個(gè)日期時(shí)間對(duì)象的“范圍”,由開始日期和停止日期定義。我想創(chuàng)建一個(gè)新的“范圍”(開始和停止日期)來匹配前兩個(gè)范圍中的日期。這是我嘗試過的,但我的列表有問題:它似乎沒有正確獲取最后一個(gè)元素?import datetime#First date range is next week from monday to sunday:next_week_day = datetime.datetime.now().date() + datetime.timedelta(days=7)monday = next_week_day - datetime.timedelta(days=next_week_day.weekday())sunday = monday + datetime.timedelta(days=7)daterange = [monday + datetime.timedelta(days=x) for x in range(0, (sunday-monday).days)]  #Second daterange can be anything:start = datetime.date(2020, 8, 2)end = datetime.date(2020, 9, 25)daterange_2 = [start + datetime.timedelta(days=x) for x in range(0, (end-start).days)]  #then I create a list to add matching days to create the new rangedate_list = []for date in daterange:  for date_2 in daterange_2:    if date_2 == date:      date_list.append(date)#and retrieve first and last day in listfirst_date = date_list[0]second_date = date_list[-1]print(first_date, second_date)print(type(first_date), type(second_date))打印'2020, 9, 24'secondary_date 而不是'2020, 9, 25'. 我缺少什么?
查看完整描述

1 回答

?
慕容3067478

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

您需要更新兩者的范圍daterange并daterange_2包含所有必要的日期:


import datetime


# First date range is next week from monday to sunday:

next_week_day = datetime.datetime.now().date() + datetime.timedelta(days=7)

monday = next_week_day - datetime.timedelta(days=next_week_day.weekday())

sunday = monday + datetime.timedelta(days=7)


daterange = [monday + datetime.timedelta(days=x) for x in range(0, (sunday - monday).days + 1)]


# Second daterange can be anything:

start = datetime.date(2020, 8, 2)

end = datetime.date(2020, 9, 25)

daterange_2 = [start + datetime.timedelta(days=x) for x in range(0, (end - start).days + 1)]


# then I create a list to add matching days to create the new range

date_list = []

for date in daterange:

    for date_2 in daterange_2:

        if date_2 == date:

            date_list.append(date)


# and retrieve first and last day in list

first_date = date_list[0]

second_date = date_list[-1]


print(first_date, second_date)

print(type(first_date), type(second_date))


查看完整回答
反對(duì) 回復(fù) 2023-08-08
  • 1 回答
  • 0 關(guān)注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報(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)