python 函数。如果键是今天的日期,则从字典中输出键列表

python function. output a keys list from a dictionary if the key is todays date

我导入日期时间模块。但我不确定如何将其称为字典键。

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())```

您可以比较您为每个键提供的信息的元组,即 yearmonthdate 与来自 [=14] 的可用信息的相同子集=]:

>>> 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']