将dict理解转换为标准形式

时间:2018-11-18 05:33:27

标签: python

我需要将其转换为普通的for循环形式

read = {
    year: {
         month: read_data_of_a_month(path) for month in months
    }
    for year in range(2000,2013)
}

2 个答案:

答案 0 :(得分:1)

只需使用嵌套的for循环:

read = {}
for year in range(2000, 2013):
    year_dict = {}
    for month in months:
        year_dict[month] = read_data_of_a_month(path)
    read[year] = year_dict

答案 1 :(得分:-1)

read = {}
for year in range(2000, 2013):
    m = {}
    for month in months:
        m[month] = read_data_of_a_month(path)
    read[year] = m