你的问题类似于:
l = [[0]] * 4
l[0][0] += 1
print l # prints "[[1], [1], [1], [1]]"
如果是这样,您只需要在存储对象时复制对象:
import copy
l = [copy.copy(x) for x in [[0]] * 4]
l[0][0] += 1
print l # prints "[[1], [0], [0], [0]]"
有问题的对象应该实现一个__copy__方法来复制对象。请参阅copy的文档。您可能也对copy.deepcopy感兴趣,这也是在那里。
编辑:这是问题:
arrayList = []
for x in allValues:
result = model(x)
arrayList.append(wM) # appends the wM object to the list
wM.reset() # clears the wM object
你需要附加一个副本:
import copy
arrayList = []
for x in allValues:
result = model(x)
arrayList.append(copy.copy(wM)) # appends a copy to the list
wM.reset() # clears the wM object
但是我仍然对于wM来自哪里感到困惑。你不会一次又一次地复制相同的wM对象,除了在第一次清除之后,剩下的所有的都将为空吗?还是模型()修改wM(这听起来像是一个可怕的设计缺陷)?为什么要扔掉结果?