Python Forum
Understanding how reassignment works
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Understanding how reassignment works
#1
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])
Reply
#2
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.
Reply
#3
(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?
Reply
#4
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.
Reply
#5
Great! Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trouble Understanding Why This Code Works crocolicious 2 2,707 Apr-09-2019, 05:24 PM
Last Post: crocolicious

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020