为什么python不会覆盖非零数组?

时间:2016-04-30 19:40:04

标签: python arrays loops logic blender

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。我想知道为什么会这样?我不明白它的逻辑。

1 个答案:

答案 0 :(得分:1)

theta1theta2列表是对象。您的初始循环修改这些对象。然后,您可以在globalDict

中存储对这些对象的引用

然而,我再次修改相同的对象显然所有引用都将指向修改后的字典。您必须为每次迭代创建新对象以避免这种情况。这就是为什么添加这两行会改变行为的原因。

您的情况与更简单的情况类似:

>>> a = []
>>> d = {}
>>> d['a'] = a
>>> a.append(1)
>>> a
[1]
>>> d['a']    # modified because d is actually storing a reference to that list.
[1]
>>> a = []    # now a is a completely different object
>>> a
[]
>>> d['a']   # old reference keep original a value alive.
[1]

另请注意, nothing 与“归零”数组有关。您可以用以下内容替换最后两行:

theta1 = [el.copy() for el in theta1]  # or list(el) for el in ...
theta2 = [el.copy() for el in theta2]  # or el[:] for el in ...

实现相同的结果,因为下一个循环将覆盖这些值。 (唯一的区别是,这不会在最后一次迭代中将theta1theta2归零......)

顺便说一下:可能最好使用numpy.random.normal生成随机数组:

>>> numpy.random.normal(size=(5,5))
array([[ 1.16883072, -1.12038842, -0.14351093,  1.20373197,  0.79088439],
       [-0.80960599, -0.56876464,  1.12962452,  0.20582962, -1.36239647],
       [-1.07225523,  0.56895514, -0.07132619,  1.36478187,  0.62836829],
       [ 0.69711124, -0.81957984, -1.27820196,  0.04203822,  1.68618401],
       [-0.54687767,  0.34994992, -0.91724856,  0.2631614 ,  0.08691433]])