阿波罗的战车
我做了一个递归函数来解决你的问题:import datetimedef find_date(date, date_dict): if date not in date_dict.keys(): return find_date(date-datetime.timedelta(days=1), date_dict) else: return date我不知道你的字典的内容是什么,但下面的例子应该向你展示它是如何工作的:import numpy as np# creates a casual dates dictionarymonths = np.random.randint(3,5,20)days = np.random.randint(1,30,20)dates = { datetime.date(2019,m,d): '{}_{:02}_{:02}'.format(2019,m,d) for m,d in zip(months,days)}# select the date to findtarget_date = datetime.date(2019, np.random.randint(3,5), np.random.randint(1,30))# print the resultprint("The date I wanted: {}".format(target_date))print("The date I got: {}".format(find_date(target_date, dates)))