我在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)
...
Run Code Online (Sandbox Code Playgroud)
如下所示,matriz
尽管aux
函数已更改了矩阵,但矩阵仍会发生变化solucoes()
.
矩阵之前:
[[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]]
brc*_*brc 69
这条线
aux=matriz;
Run Code Online (Sandbox Code Playgroud)
不制作副本matriz
,它只是创建一个matriz
名为的新引用aux
.你可能想要
aux=matriz[:]
Run Code Online (Sandbox Code Playgroud)
这将是一个副本,假设matriz
是一个简单的数据结构.如果它更复杂,你应该使用copy.deepcopy
aux = copy.deepcopy(matriz)
Run Code Online (Sandbox Code Playgroud)
顺便说一句,在每个语句之后你不需要分号,python不会将它们用作EOL标记.
She*_*har 17
使用复制模块
aux = copy.deepcopy(matriz) # there is copy.copy too for shallow copying
Run Code Online (Sandbox Code Playgroud)
次要的:不需要分号.
归档时间: |
|
查看次数: |
64145 次 |
最近记录: |