首页 > 解决方案 > AttributeError:“列表”对象没有属性“副本”。在 3.6 中工作,在 3.7 中失败

问题描述

代码摘录:

toMatrix  = [0.0] * NumBoth  #Establish dimensions
for i in range(NumBoth):
    toMatrix [i] = [0.0] * NumBoth
for indx in iter(range(NumSvcDep,NumBoth)):
    toMatrix [indx][indx] = 1.0
print(type(toMatrix),type(fromMatrix))
fromMatrix = toMatrix.copy()

如果上下文很重要,我将矩阵设置为“0”,但对角线的一部分设置为“1”。虽然我很想听到更好的方法来做到这一点,但我的主要问题是复制。这在 V3.6 上运行,但在 v3.7 上失败。我很确定代码没有更改,但已经有一段时间了。

标签: python

解决方案


Deep copy of list works a little different:

fromMatrix = toMatrix[:]

should solve your error, Alternatively, you can do:

fromMatrix = list(toMatrix)

推荐阅读