In the following code:
E=np.zeros((2,2))
H=np.zeros((2,2)) 1
L=[]
L.append(E)
E=H
L.append(E)
print(L)
the output is what I expect to be:
[array([[0., 0.],
[0., 0.]]), array([[1., 1.],
[1., 1.]])]
that is L[0] remains the same after the second appending. However, for the code:
E=np.zeros((2,2))
H=np.zeros((2,2)) 1
L=[]
L.append(E)
E[0,0]=H[0,0]
L.append(E)
print(L)
the output is
[array([[1., 0.],
[0., 0.]]), array([[1., 0.],
[0., 0.]])]
that is L[0] was changed after the second appending. can someone explain why there is difference? I know that appending E.copy() is more safe but I do not understand what is the source for the difference since E[0,0] is numpy and not list.
CodePudding user response:
In your first example the variable E
gets assigned to the same array as H
refers to (the second array). The original relation of E
to the first array is simply overwritten. That's why the first list element is unaffected in the first example.
In your second example, you change the content of the array to which E
refers to and to which the first entry of that list has also be assigned to. Therefore it changes.
The action of =
in python is rather explicit. Just assign some reference to a variable. Whatever that variable referred to before, simply does not matter.