我在Python中遇到了一个非常奇怪的简单问题。
def estExt(matriz,erro):
# (1) Determinar o vector X das soluções
print ("Matrix after:");
print(matriz);
aux=matriz;
x=solucoes(aux); # IF aux is a copy of matrix, why the matrix is changed??
print ("Matrix before: ");
print(matriz)
...
如下所示,尽管aux
是由函数solucoes()
更改的矩阵,但矩阵matriz
已更改。
前矩阵:[[7, 8, 9, 24], [8, 9, 10, 27], [9, 10, 8, 27]]
矩阵后:[[7, 8, 9, 24], [0.0, -0.14285714285714235, -0.2857142857142847, -0.42857142857142705],
[0.0, 0.0, -3.0, -3.0000000000000018]]
线路
不复制
matriz
,它只创建名为aux
的对matriz
的新引用。你可能想要假设}
matriz
是一个简单的数据结构,它将生成一个副本。如果它更复杂,您可能应该使用^{另外,在每个语句后面不需要分号,python不使用它们作为EOL标记。
使用copy模块
小号:不需要分号。
aux
不是matrix
的副本,只是引用同一对象的不同名称。使用copy module创建对象的实际副本。
相关问题 更多 >
编程相关推荐