Python。无法从 for 循环向嵌套字典添加值
Python. Trouble adding values to nested Dictionaries from a for loop
我想我有两个问题。第一个是 "is the path I'm going down below the right way, and if not, what should I be doing?",第二个是 "how do I stop overwriting the values"。
在下面的代码中,如何使 "monthdict" 输出与 "report" 相同?
报告是一个值列表,其中(例如)1 月第 1 周星期一面包为 1,但 2 月第 1 周星期一面包为 31。当我尝试以编程方式重新创建它时,它使所有面包都为 53。
我的真实使用情况有来自 API 的实际数据,我一次只能从中获取 7 个项目。所以我需要按顺序调用 1 月 1 周星期一、第 1 周星期二、第 1 周星期三、1 月 2 周星期一等,然后 return 值
report = {
"january" : {
"week1" : {
"monday" : {
"bread" : 1,
"cheese" : 2,
"milk" : 3
},
"tueday" : {
"bread" : 4,
"cheese" : 5,
"milk" : 6
},
"wednesday" : {
"bread" : 7,
"cheese" : 8,
"milk" : 9
},
},
"week2" : {
"monday" : {
"bread" : 11,
"cheese" : 12,
"milk" : 13
},
"tueday" : {
"bread" : 14,
"cheese" : 15,
"milk" : 16
},
"wednesday" : {
"bread" : 17,
"cheese" : 18,
"milk" : 19
},
},
"week3" : {
"monday" : {
"bread" : 21,
"cheese" : 22,
"milk" : 23
},
"tueday" : {
"bread" : 24,
"cheese" : 25,
"milk" : 26
},
"wednesday" : {
"bread" : 27,
"cheese" : 28,
"milk" : 29
},
},
},
"february" : {
"week1" : {
"monday" : {
"bread" : 31,
"cheese" : 32,
"milk" : 33
},
"tueday" : {
"bread" : 34,
"cheese" : 35,
"milk" : 36
},
"wednesday" : {
"bread" : 37,
"cheese" : 38,
"milk" : 39
},
},
"week2" : {
"monday" : {
"bread" : 111,
"cheese" : 112,
"milk" : 113
},
"tueday" : {
"bread" : 114,
"cheese" : 115,
"milk" : 116
},
"wednesday" : {
"bread" : 117,
"cheese" : 118,
"milk" : 119
},
},
"week3" : {
"monday" : {
"bread" : 121,
"cheese" : 122,
"milk" : 123
},
"tueday" : {
"bread" : 124,
"cheese" : 125,
"milk" : 126
},
"wednesday" : {
"bread" : 127,
"cheese" : 128,
"milk" : 129
},
}
}
}
print("\nreport:\n", report, "\n\n")
months = ["january", "february"]
weeks = ["week1", "week2", "week3"]
days = ["monday", "tuesday", "wednesday"]
food = ["bread", "cheese", "milk"]
values = []
for i in range(1,129):
values.append(i)
x = 0
fooddict = {}
daydict = {}
weekdict = {}
monthdict= {}
for m in months:
for w in weeks:
for d in days:
for f in food:
fooddict[f] = values[x]
x += 1
print(x, ") adding ", values[x], " to ", f)
daydict[d] = fooddict
weekdict[w] = daydict
monthdict[m] = weekdict
print(monthdict)
试试这个
monthdict= {}
for m in months:
weekdict = {}
for w in weeks:
daydict = {}
for d in days:
fooddict = {}
for f in food:
fooddict[f] = values[x]
x += 1
print(x, ") adding ", values[x], " to ", f)
daydict[d] = fooddict
weekdict[w] = daydict
monthdict[m] = weekdict
否则,您将共享前一个月、前一周和前几天的值(相反,对于每个新迭代,它们应该从空开始)。
我想我有两个问题。第一个是 "is the path I'm going down below the right way, and if not, what should I be doing?",第二个是 "how do I stop overwriting the values"。
在下面的代码中,如何使 "monthdict" 输出与 "report" 相同?
报告是一个值列表,其中(例如)1 月第 1 周星期一面包为 1,但 2 月第 1 周星期一面包为 31。当我尝试以编程方式重新创建它时,它使所有面包都为 53。
我的真实使用情况有来自 API 的实际数据,我一次只能从中获取 7 个项目。所以我需要按顺序调用 1 月 1 周星期一、第 1 周星期二、第 1 周星期三、1 月 2 周星期一等,然后 return 值
report = {
"january" : {
"week1" : {
"monday" : {
"bread" : 1,
"cheese" : 2,
"milk" : 3
},
"tueday" : {
"bread" : 4,
"cheese" : 5,
"milk" : 6
},
"wednesday" : {
"bread" : 7,
"cheese" : 8,
"milk" : 9
},
},
"week2" : {
"monday" : {
"bread" : 11,
"cheese" : 12,
"milk" : 13
},
"tueday" : {
"bread" : 14,
"cheese" : 15,
"milk" : 16
},
"wednesday" : {
"bread" : 17,
"cheese" : 18,
"milk" : 19
},
},
"week3" : {
"monday" : {
"bread" : 21,
"cheese" : 22,
"milk" : 23
},
"tueday" : {
"bread" : 24,
"cheese" : 25,
"milk" : 26
},
"wednesday" : {
"bread" : 27,
"cheese" : 28,
"milk" : 29
},
},
},
"february" : {
"week1" : {
"monday" : {
"bread" : 31,
"cheese" : 32,
"milk" : 33
},
"tueday" : {
"bread" : 34,
"cheese" : 35,
"milk" : 36
},
"wednesday" : {
"bread" : 37,
"cheese" : 38,
"milk" : 39
},
},
"week2" : {
"monday" : {
"bread" : 111,
"cheese" : 112,
"milk" : 113
},
"tueday" : {
"bread" : 114,
"cheese" : 115,
"milk" : 116
},
"wednesday" : {
"bread" : 117,
"cheese" : 118,
"milk" : 119
},
},
"week3" : {
"monday" : {
"bread" : 121,
"cheese" : 122,
"milk" : 123
},
"tueday" : {
"bread" : 124,
"cheese" : 125,
"milk" : 126
},
"wednesday" : {
"bread" : 127,
"cheese" : 128,
"milk" : 129
},
}
}
}
print("\nreport:\n", report, "\n\n")
months = ["january", "february"]
weeks = ["week1", "week2", "week3"]
days = ["monday", "tuesday", "wednesday"]
food = ["bread", "cheese", "milk"]
values = []
for i in range(1,129):
values.append(i)
x = 0
fooddict = {}
daydict = {}
weekdict = {}
monthdict= {}
for m in months:
for w in weeks:
for d in days:
for f in food:
fooddict[f] = values[x]
x += 1
print(x, ") adding ", values[x], " to ", f)
daydict[d] = fooddict
weekdict[w] = daydict
monthdict[m] = weekdict
print(monthdict)
试试这个
monthdict= {}
for m in months:
weekdict = {}
for w in weeks:
daydict = {}
for d in days:
fooddict = {}
for f in food:
fooddict[f] = values[x]
x += 1
print(x, ") adding ", values[x], " to ", f)
daydict[d] = fooddict
weekdict[w] = daydict
monthdict[m] = weekdict
否则,您将共享前一个月、前一周和前几天的值(相反,对于每个新迭代,它们应该从空开始)。