我导入datetime模块。但我不确定如何将其称为字典键。
def bad_food():
today = datetime.date.today()
past_due = {
datetime.date.today(2020,11,24): ['chicken' , 'soup', 'chips'],
datetime.date.today(2020,11,27) : ['lays', 'chilli'],
}
for key in past_due:
if key == today:
print (today.values())
print(bad_food())```
你可以将为每个键提供的信息元组(即和)进行比较year
,month
并将date
其与来自的可用信息的相同子集进行比较today
:
>>> import datetime
>>>
>>> def bad_food():
... today = datetime.datetime.now()
... past_due = {
... datetime.datetime(2020, 11, 24): ['chicken', 'soup', 'chips'],
... datetime.datetime(2020, 11, 27): ['lays', 'chilli'],
... }
... for key in past_due:
... if (key.year, key.month, key.day) == (today.year, today.month, today.day):
... print(past_due[key])
...
>>> bad_food()
['lays', 'chilli']
太好了,谢谢您的帮助