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


Messages In This Thread
Understanding how reassignment works - by Shreya10o - May-09-2020, 10:50 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trouble Understanding Why This Code Works crocolicious 2 2,735 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