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

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

打印特定值等于單獨(dú)的指定標(biāo)準(zhǔn)的指定詞典

打印特定值等于單獨(dú)的指定標(biāo)準(zhǔn)的指定詞典

慕斯王 2023-01-04 10:12:17
我想要做的只是從日期值在特定月份內(nèi)的列表中提取字典。from datetime import date, datetimelistexample = []def examplefunc():  listexample.append(    {'example_record':len(examplelist)+1,     'date':datetime.strptime(input('Date (yyyy/mm/dd): ' ), '%Y/%m/%d'),    })examplefunc()for i in listexample:  print(listexample)如果我將月份標(biāo)準(zhǔn)指定為“3”,則只有日期中的月份等于“3”的字典才會(huì)產(chǎn)生/打印。如果我在請(qǐng)求輸入時(shí)輸入“2020/02/01”,輸出解析為:[{'example_record': 4, 'date': datetime.datetime(2020, 2, 1, 0, 0)}]月份在字典條目中(如上例中的“2”),但我只想提取那些“日期”為==特定月份(或當(dāng)前月份)的字典條目。我已經(jīng)嘗試遍歷字典中指定鍵值的列表,但這似乎也不起作用。這是我一直在嘗試的示例:def show_all_in_current_month():while True:     (examplelist[0]['date']).month == '{:%m}'.format(date.today())    for i in range(len(examplelist)):      print(examplelist)    break任何幫助將不勝感激。謝謝!編輯:這個(gè)(難以忍受的簡(jiǎn)單)解決方案適用于我的問(wèn)題。謝謝大家的貢獻(xiàn)。for d in examplelist: if str(d['date'].month) == '2':  print(d)
查看完整描述

1 回答

?
白衣染霜花

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

你可以嘗試這樣的事情:


from datetime import datetime

listexample=[]

def examplefunc():

  listexample.append(

    {'example_record':len(listexample)+1,

     'date':datetime.strptime(input('Date (yyyy/mm/dd): ' ), '%Y/%m/%d'),

    })


#Define how many dates are you going to add 

for i in range(int(input("How many dates are you going to add?\n:"))):

   examplefunc()


def show_all_in_current_month(currentmonth):   #receives the a month and filter the dicts that are in that month

   print([dict for dict in listexample if dict['date'].month==currentmonth])

show_all_in_current_month(int(input("Which month do you want to filter the list with?\n:")))


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

添加回答

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