我正在尝试构建一个可以更改大小并发布到Firebase的数据结构。我看到的问题是在构建数据结构期间。我写了以下代码:
for i in range(len(results)):
designData = {"Design Flag" : results[i][5],
"performance" : results[i][6]}
for j in range(len(objectiveNameArray)):
objectives[objectiveNameArray[j]] = results[i][columnHeaders.index(objectiveNameArray[j])]
designData["objectives"] = copy.copy(objectives)
for k in range(len(variableNameArray)):
variables[variableNameArray[k]] = results[i][columnHeaders.index(variableNameArray[k])]
designData["variables"] = copy.copy(variables)
for l in range(len(responseNameArray)):
responses[responseNameArray[l]] = results[i][columnHeaders.index(responseNameArray[l])]
designData["responses"] = copy.copy(responses)
for m in range(len(constraintNameArray)):
constraintViolated = False
if constraintNameArray[m][1] == "More than":
if results[i][columnHeaders.index(constraintNameArray[m][0])] > constraintNameArray[m][2]:
constraintViolated = True
else:
constraintViolated = False
elif constraintNameArray[m][1] == "Less than":
if results[i][columnHeaders.index(constraintNameArray[m][0])] < constraintNameArray[m][2]:
constraintViolated = True
else:
constraintViolated = False
if constraintNameArray[m][0] in constraints:
if constraints[constraintNameArray[m][0]]["violated"] == True:
constraintViolated = True
constraints[constraintNameArray[m][0]] = {"value" : results[i][columnHeaders.index(constraintNameArray[m][0])], "violated" : constraintViolated}
designData["constraints"] = copy.copy(constraints)
data[studyName][results[i][4]] = designData
当我在for循环中包含print(designData)时,我发现每次循环迭代都会按预期更改结果。
但是,如果我在for循环之外包含print(数据),我会得到一个数据结构,其中结果数组添加的值对于循环的每次迭代都是相同的值,即使键是不同的。
Comparing print(data) and print(designData)
我提前道歉,如果这不是足够的信息,这是我在Stack上的第一篇文章,请耐心等待。
答案 0 :(得分:0)
可能是因为您将$good += $values -join '|'
,objectives
,variables
等变量直接添加到responses
。请尝试以下方法:
designData
有关类似问题,请参阅copy a list。