【发布时间】:2017-03-15 21:58:12
【问题描述】:
为了理解为什么我在程序中遇到错误,我试图在其中找到行列式的“次要”,我编写了一个更简单的程序,因为我的变量搞砸了。下面的这个函数接受一个 2 * 2 矩阵作为输入,并返回一个包含其行的列表(我知道毫无意义且效率低下,但我试图理解这背后的理论)。
def alpha(A): #where A will be a 2 * 2 matrix
B = A #the only purpose of B is to store the initial value of A, to retrieve it later
mylist = []
for i in range(2):
for j in range(2):
del A[i][j]
array.append(A)
A = B
return mylist
然而,这里似乎 B 被分配了 A 的值 动态,从某种意义上说,我无法将 A 的初始值存储在 B 中以供以后使用。这是为什么呢?
【问题讨论】:
-
array.append(A)真的应该是mylist.append(A)吗?如果没有,array定义在哪里? -
恐怕您从文档中复制了 array.append(A) 而没有考虑到“array”是您要附加到的实际数组,即与该数组对应的变量,而不是“array” “ 包裹。如果我错过了什么对不起?
-
为了获得漂亮的可视化效果,请将 [one] 与 [two] 进行比较。 [1]:pythontutor.com/… [2]:pythontutor.com/…
标签: python python-2.7 variables reference deep-copy