-1

Suppose a 2d numpy array as the following:

A = np.ones((5,5))

Now, I want to update (change) all the values of A using the following code:

for row in A: for entry in row: entry = 999

After this, the entries en A are 1, i.e. the matrix is the same as the original.

But, If I run this code, the values are changed:

for i,row in enumerate(A): for j,entry in enumerate(row): A[i][j] = 999 Why this is happening?

Is the cell variable a copy and not a pointer?

Juan
  • 1,022
  • 9
  • 16
  • A concurrent question with the same issue, but for a list: http://stackoverflow.com/questions/35355739/multiply-every-element-of-a-list-by-a-number – hpaulj Feb 12 '16 at 22:49

1 Answers1

1

That is because when you reassign entry, you are simply redefining a variable. When you first define entry, you are assigning to it a value, not a position in the array.

zondo
  • 19,901
  • 8
  • 44
  • 83
  • Thank you! I was very confused because I was thinking on how python points to references and not values. – Juan Feb 12 '16 at 22:19