我在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
也被修改了。我不明白为什么。在
因为您将
X
分配给Y
。这意味着Y
是对X
引用的引用!如果不想复制X
:相关问题 更多 >
编程相关推荐