[英]Printing nested dictionary loop YYYYMMDD
請幫助,這里的新手。 我想遍歷一個月中的每一天,並以 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])
你應該考慮重新構建你的字典,你的數據結構有冗余數據並且不支持每月多天。 主字典可能有幾年作為這樣的鍵:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.