0
我在python中有這個問題。我有采取以下輸入在python中,這個問題是什麼?
import numpy;
from numpy import *;
def GetInf(G, X, m, n):
g = G[m - 1, :].T;
Y = X;
Y[m - 1, :] = 0;
Y[:, n - 1] = 0;
# Here I modify Y. The problem is that X is modified too. Why?
# In fact, I add Y after I see that X is changing but X keeps changing.
result = sum(Y * G);
return result;
G = array([[1., 2., 3.], [4., 5., 6.]]);
X = array([[1., 0., 0.], [0., 0., 1.]]);
I = GetInf(G, X, 1, 1);
我的問題是一個功能,當我調試程序我看到修改後Y
,X
也被修改。我不明白爲什麼。
'Y'和'X'是指向同一個對象的名字,如果你想副本嗎:'Y = X。副本()'。 –
您將X分配給Y變量。它與複製矩陣不同,但分配矩陣對象本身。所以改變x與改變y相同。嘗試調用'Y = x.copy()' –
這是一篇博客文章,解釋如何在python中使用變量賦值。 http://foobarnbaz.com/2012/07/08/understanding-python-variables/ – Dunes