变量在同一循环迭代中获取新值

时间:2015-02-09 18:40:58

标签: python variables dictionary scope renaming

我遇到了一个问题,我在循环顶部设置的变量基本上是在迭代结束之前获得一个新值。我如何改变它以便它不会发生?我认为这与范围有关,但我并不完全确定。相关代码和输出如下:

while not stop(old_centers, centers): #returns True when old centers == new ones

    old_centers = centers #setting old centers to centers, nothing fancy
    print 'old_centers', old_centers

    centers = label(points, centers)#calls the label func and assigns centers a new value

    print 'old centers ', old_centers, '\n new ones ', centers

    print '# of iterations: %d' % iterations
    iterations += 1

这是输出:

  

初始中心{' 1':[1.193013180404786,1.3490890491331484],' 0':[0.10173245995124558,0.2259745323785607],' 2':[1.1969102753605907,1.1584028826056012] }

     

old_centers {' 1':[1.193013180404786,1.3490890491331484],' 0':[0.10173245995124558,0.2259745323785607],' 2':[1.1969102753605907,1.15584028826056012]}

     

新标签中心{' 1':[1.1646709311677974,1.7595898757959954],' 0':[0.23028481023828123,0.9213541910575308],' 2':[1.3055269036979071, 0.6867418777771471]}

     

旧中心{' 1':[1.1646709311677974,1.7595898757959954],' 0':[0.23028481023828123,0.9213541910575308],' 2':[1.3055269036979071,0.6867418777771471] }

     

新的{' 1':[1.1646709311677974,1.7595898757959954],' 0':[0.23028481023828123,0.9213541910575308],' 2':[1.3055269036979071,0.6867418777771471] }

     

迭代次数:0

如您所见,我的 old_centers 变量在循环结束时被更改,强制while循环在第一次迭代后取消。首先,它正确接收初始字典,但在调用标签后,它会发生变化,我不知道为什么。有关为何发生这种情况的任何提示?

1 个答案:

答案 0 :(得分:1)

您需要copy.deepcopy,只需使用的副本,因为您将列表作为值,因此任何列表中的任何更改也会在old_centers中更改:

from copy import deepcopy

old_centers = deepcopy(centers)

old_centers = centers创建对centers的引用,因此对centers的任何更改都会反映在old_centers中,因为两个对象都指向内存中的相同位置,这两个实际上都是同一个对象。 c is d

d = {1:2}
c = d
d[1] = 4
print(id(c),id(d))
print(c,d)
print(c is d)

140661961276040 140661961276040 # same id same objects
{1: 4} {1: 4}
True

现在复制:

from copy import deepcopy
d = {1: 2}
c = deepcopy(d) # create new object
d[1]= 4
print(id(c),id(d))
print(c,d)
print(c is d) 

140477200628872 140477200628360 # now two different id's two different objects
{1: 2} {1: 4}
False