Changing value of a 2D array element changes the complete column
Asked Answered
E

1

5

When I print my arr value i get the correct values for my 2D array but when I exit the while loop my values are all wrong.

I am not sure what i am doing wrong.

    #num runs
    n = 4

    x = np.linspace(-1,1,n)
    y = np.linspace(-1,1,n)
    x1,y1 = np.meshgrid(x, y)

    l = np.linspace(0,1000,n)
    x = np.linspace(-1,1,n)
    p1,p2 = np.meshgrid(l,l)

    w020 = 5*(y1**2+x1**2)

    row, cols = (n,n)
    arr = [[0]*cols]*row
  
    i = 0
    p = 0
    while i < n:
        i += 1
        p=0
        while p < n:
            arr[i-1][p] = 2+2*math.cos(2*math.pi*w020[i-1,p])
            p += 1
    print(arr)       
Educatory answered 19/6, 2020 at 23:54 Comment(4)
Can you explain what value you are expecting for arr ?Though
When I print my arr[i-1][p] value in the while loop i get the values i am expecting which for 4 runs would be 4.0 0.12061475842817959 0.12061475842817959 4.0 0.12061475842817959 3.532088886237954 3.5320888862379562 0.12061475842817959 0.12061475842817959 3.5320888862379562 3.53208888623796 0.12061475842817959 4.0 0.12061475842817959 0.12061475842817959 4.0 But when the loop is complete and i graph and print my array i do not get those values for my array elements.Educatory
I ran your code and the value are as you require, the final array is ``` [[4.0, 0.12061475842817959, 0.12061475842817959, 4.0], [4.0, 0.12061475842817959, 0.12061475842817959, 4.0], [4.0, 0.12061475842817959, 0.12061475842817959, 4.0], [4.0, 0.12061475842817959, 0.12061475842817959, 4.0]] ```Though
Inside the loop my array values for the arr[1] are as follows [0.12061475842817959 3.532088886237954 3.5320888862379562 0.12061475842817959] outside the loop my values change to the values you mentioned i am not sure why my values are changing when i exit the loop if you print the values as they are being calculated inside the loop i get the values i am after. Outside the loop my values change.Educatory
T
11

The way you created the 2D array, creates a shallow list

    arr = [[0]*cols]*row

Instead, if you want to update the elements of the list, you should use

   rows, cols = (5, 5) 
   arr = [[0 for i in range(cols)] for j in range(rows)] 

Explanation:

One can create a list using :

   arr = [0]*N 

or

   arr = [0 for i in range(N)] 

In the first case all the indices of the array point to the same integer object

enter image description here

and when you assign a value to a particular index, a new int object is created, for eg arr[4] = 5 creates

enter image description here

Now let us see what happens when we create a list of list, in this case, all the elements of our top list will point to the same list

enter image description here

And if you update the value of any index a new int object will be created. But since all the top-level list indexes are pointing at the same list, all the rows will look the same. And you will get the feeling that updating an element is updating all the elements in that column.

enter image description here

Credits: Thanks to Pranav Devarakonda for the easy explanation here

Though answered 21/6, 2020 at 11:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.