Array Assignment
When assigning NumPy arrays, you have to take note of how arrays are assigned. Following are a number of examples to illustrate this.
Copying by Reference
Consider an array named a1:
list1 = [[1,2,3,4], [5,6,7,8]]a1 = np.array(list1)print(a1)'''[[1 2 3 4][5 6 7 8]]'''
When you try to assign a1 to another variable, a2, a copy of the array is created:
a2 = a1 # creates a copy by referenceprint(a1)'''[[1 2 3 4][5 6 7 8]]'''print(a2)'''[[1 2 3 4][5 6 7 8]]'''
However, a2 is actually pointing to the original a1. So, any changes made to either array will affect the other as follows:
a2[0][0] = 11 # make some changes to a2print(a1) # affects a1'''[[11 2 3 4][ 5 6 7 8]]'''print(a2)'''[[11 2 3 4][ 5 6 7 8]]'''