请帮助,新手在这里。我想遍历一个月中的每一天,并每天以YYYYMMDD格式打印日期,例如。 20150203(2015年2月的第三天)。
initial_values = {'year': 2015}
calendar_5 = {initial_values['year']: {"01": 31, "02": 28}}
for day in range(calendar_5[initial_values['year']]["02"]):
for year in calendar_5:
for month, day in calendar_5.items():
print(calendar_5[initial_values[month]][day])
答案 0 :(得分:1)
您应该考虑重新构造字典,您的数据结构具有冗余数据,并且每月不支持多天。主字典可能以年份作为关键字,例如:
year: month_dict
month_dict
均以月为键,以月日列表为值。 例如,您需要的字典可能像这样:
my_dict = {
'2019': {
'1': [2, 14, 30], # January
'5': [5, 11, 13] # May
}
'2020': {
'12': [9] # December
}
}
那样,打印它真的很容易:
for year, month_dict in my_dict.items():
for month, list_days in month_dict.items():
for day in list_days:
print('{}{}{}'.format(year, month, day)