python中的变量矫饰是什么问题?

2024-10-02 22:33:16 发布

您现在位置: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也被修改了。我不明白为什么。在


Tags: the函数fromimportnumpythathereis