[英]Python. Trouble adding values to nested Dictionaries from a for loop
I guess I have two questions.我想我有两个问题。 First is "is the path I'm going down below the right way, and if not, what should I be doing?"
首先是“我要走的路是否在正确的道路下方,如果不是,我该怎么办?” and second is "how do I stop overwriting the values".
其次是“我如何停止覆盖值”。
In the code below, how do I make "monthdict" output the same as "report"?在下面的代码中,如何使“monthdict”输出与“report”相同?
Report is a list of values where (eg) january week1 monday bread is 1, but february week1 monday bread is 31. When I try to recreate it programattically it makes ALL the breads 53.报告是一个值列表,其中(例如)1 月第 1 周周一面包为 1,但 2 月第 1 周周一面包为 31。当我尝试以编程方式重新创建它时,它使所有面包为 53。
My real world usage has actual data that's coming in from an API that I can only 7 items from at a time.我在现实世界中的使用有来自 API 的实际数据,我一次只能从中获取 7 个项目。 So I need to call jan week 1 monday, week 1 tuesday, week 1 wednesday in order, jan week 2 monday etc. repeatedly and return the values
所以我需要按顺序调用 jan 周第 1 周、第 1 周周二、第 1 周周三、jan 周 2 周一等重复并返回值
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)
Try this尝试这个
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
Otherwise, you are sharing the values from the previous month, week, and days (instead, they should start as empty for each new iteration).否则,您将共享前一个月、一周和几天的值(相反,对于每个新迭代,它们应从空开始)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.