for k in range(0, popSiz):
for i in range(0, mn1[0]):
for j in range(0,mn1[1]):
theta1[i][j] = random.gauss(0, 1)
for i in range(0, mn2[0]):
for j in range(0,mn2[1]):
theta2[i][j] = random.gauss(0, 1)
GameLogic.globalDict["theta1-" + str(k)] = theta1
GameLogic.globalDict["theta2-" + str(k)] = theta2
theta1 = [[0 for x in range(mn1[1])] for y in range(mn1[0])] # this
theta2 = [[0 for x in range(mn2[1])] for y in range(mn2[0])] # and this
print(GameLogic.globalDict["theta1-0"]==GameLogic.globalDict["theta1-1"])
如果我注释最后两行(它说# this
# and this
),那么所有的.globalDict["theta1-" + str(k)]
都是一样的。所以如果它被注释,它会打印true,如果它没有被注释,它会打印false。我想知道这是为什么?我不明白它的逻辑
theta1
和theta2
列表是对象。初始循环修改这些对象。然后将对这些对象的引用存储在globalDict
中不过,如果您再次修改相同的对象,显然所有引用都将指向一个修改过的字典。为了避免这种情况,您必须为每次迭代创建新对象。这就是为什么加上这两条线会改变行为
您的情况类似于更简单的:
还要注意,它与“归零”数组没有任何关系。您可以将最后两行替换为:
为了获得相同的结果,因为下一个循环将覆盖这些值。 (唯一的区别是这不会在上一次迭代中将
theta1
和theta2
归零…)顺便说一下:使用^{} 生成随机数数组可能会更好:
相关问题 更多 >
编程相关推荐