Python Forum
Understanding how reassignment works - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Understanding how reassignment works (/thread-26675.html)



Understanding how reassignment works - Shreya10o - May-09-2020

I tried the following code, the for loop was not changing the original variables but only printing out the changed results. Meaning, when I printed out list4[0], it was giving the correct outcome, but not when I print out the variable in list4[0], ie,labels01112. But a reassignment of list4 changed the original variables as well. So my question is, before this reassignment, when we printed out these variables, they weren't changed, and we are assigning these unchanged variables to list4, then how is the reassignment changing the them?

labels01112=np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0])

centers01112=np.array([[ 0.01943914,  0.01259848, -0.12696217],
       [-0.9525181 , -0.61732533,  6.22114648]])

length01112=np.array([0.12905811, 6.32384731])

labels11112=np.array([1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0,
       0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1,
       1, 0, 0, 0, 0, 1])

centers11112=np.array([[-0.57971121, -0.67466955, -0.63219157],
       [ 0.49382807,  0.57471851,  0.53853356]])

length11112=np.array([1.09128836, 0.92961601])

list3=([centers01112, length01112],
[centers11112, length11112])

z=[]
for i in range(len(list3)):
  if (len([j for j in list3[i][0][0] if j < 0])<(len(list3[i][0][0])/2)) & (len([i for i in list3[i][0][1] if i < 0])>(len(list3[i][0][1])/2)):
     z.append('turn opp')
  elif (len([j for j in list3[i][0][0] if j < 0])>(len(list3[i][0][0])/2)) & (len([i for i in list3[i][0][1] if i < 0])<(len(list3[i][0][1])/2)):
     z.append('keep same')
z
Output:
['turn opp', 'keep same']
list4=[labels01112, labels11112]
for i in range(len(list4)):
  if z[i]=='turn opp' :
    idx = [1,0]
    lut = np.zeros_like(idx)
    lut[idx] = np.arange(2)
    list4[i]=lut[list4[i]]
    print(list4[i])
Output:
[1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
list4[0]
Output:
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
labels01112
Output:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
labels01112, labels11112 = list4
labels01112
Output:
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])



RE: Understanding how reassignment works - ThomasL - May-09-2020

The variables labels01112 and labels11112 are pointers to numpy arrays in memory.
With
list4 = [labels01112, labels11112]
you create a list with two pointers pointing to the numpy arrays.
With
list4[i] = lut[list4[i]]
you create a new object on the right side and replace the pointer which is pointing to labels01112 with
the pointer which is pointing to this new object.
So the numpy array is not modified by any means.
The pointer with name labels01112 is still pointing to the original numpy array.


RE: Understanding how reassignment works - Shreya10o - May-09-2020

(May-09-2020, 06:07 PM)ThomasL Wrote: The variables labels01112 and labels11112 are pointers to numpy arrays in memory.
With
list4 = [labels01112, labels11112]
you create a list with two pointers pointing to the numpy arrays.
With
list4[i] = lut[list4[i]]
you create a new object on the right side and replace the pointer which is pointing to labels01112 with
the pointer which is pointing to this new object.
So the numpy array is not modified by any means.
The pointer with name labels01112 is still pointing to the original numpy array.

Right, I got it! Thanks! So is there a way to write the for loop in such a way that the variables are amended as well, or is reassigning them later on the only way?


RE: Understanding how reassignment works - ThomasL - May-10-2020

Instead of
list4[i] = lut[list4[i]]
you could do
for idx, value in enumerate(lut[list4[i]):
    list4[i][idx] = value
but that would be very inefficient.
Reassigning is imho the better way to do it.


RE: Understanding how reassignment works - Shreya10o - May-14-2020

Great! Thanks!