0

When running this specific block of my script, record[r] is changing its value. Here're some lines I get printed out:

record[r] [[70, 190, 526, 9], [100, 160, 354, 60], [61, 45, 276, 15], [45, 61, 454, 28], [254, 192, 15, 20]] r : 0
[[190, 70, 524, 15], [160, 100, 353, 60], [45, 61, 280, 15], [45, 61, 456, 25], [245, 186, 14, 24]]
record[r] [[190, 70, 524, 15], [160, 100, 353, 60], [45, 61, 280, 15], [45, 61, 456, 25], [245, 186, 14, 24]] r : 0
[[190, 70, 528, 18], [100, 160, 355, 69], [45, 61, 277, 17], [45, 61, 454, 23], [233, 184, 9, 27]]
record[r] [[190, 70, 528, 18], [100, 160, 355, 69], [45, 61, 277, 17], [45, 61, 454, 23], [233, 184, 9, 27]] r : 0
[[190, 70, 526, 16], [160, 100, 354, 66], [45, 61, 277, 11], [61, 45, 450, 17], [242, 181, 6, 37]]
record[r] [[190, 70, 526, 16], [160, 100, 354, 66], [45, 61, 277, 11], [61, 45, 450, 17], [242, 181, 6, 37]] r : 0
[[190, 70, 531, 8], [100, 160, 358, 72], [61, 45, 280, 8], [45, 61, 448, 7], [240, 178, 4, 28]]
record[r] [[190, 70, 531, 8], [100, 160, 358, 72], [61, 45, 280, 8], [45, 61, 448, 7], [240, 178, 4, 28]] r : 0
[[190, 70, 531, 5], [100, 160, 360, 71], [45, 61, 277, 9], [45, 61, 452, 12], [238, 175, 8, 20]]
record[r] [[190, 70, 531, 5], [100, 160, 360, 71], [45, 61, 277, 9], [45, 61, 452, 12], [238, 175, 8, 20]] r : 0

Code:

for i in range(10):

    print "loop {} of 100".format(i)

    for r in range(3):

        boo = False


        while boo == False:

            print "record[r]",record[r],"r :",r
            data = place2(record[r])

            print(data)

            if validate(data, True):
                boo = True


        print "GETTING PAST WHILE"

        record, gen2 = measure2(data, gen2, record)


def place2(inp):

out = inp
for i in range(4):
    n = randint(0,1)
    if n == 1:
        out[i] = flip(out[i])

    out[i][2] += randint(-5,5)
    out[i][3] += randint(-10,10)

out[4][2] += randint(-5,5)
out[4][3] += randint(-10,10)
out[4][1] += randint(-10,10)
out[4][0] += randint(-15,15)

return out

def validate(inp, check):



p = 0
q = 0
r = 0
s = 0

for i in range(5):
    for j in range(5):

        if i != j:
            if inp[i][2] - inp[j][2] <= (-1 * inp[i][0] )or inp[i][2] - inp[j][2] >= inp[j][0]:
                p +=1
            if inp[i][3] - inp[j][3] <= (-1 * inp[i][1]) or inp[i][3] - inp[j][3] >= inp[j][1]:
                q += 1

            if inp[i][2] >= 0 and inp[i][2] <= 600 - inp[i][0]:
                r +=1
            if inp[i][3] >= 0 and inp[i][3] <= 225 - inp[i][1]:
                s +=1

if check:
    print(p,q,r,s)

if p == 20 and s + r == 40:
    return True
else:
    return False

Its also worth nothing that I never get GETTING PAST WHILE printed out so I know the culprit must be in the while loop.

record[r] should be static during the while loop and I can't explain for the life of me why it's not. I've isolated out the validate function to see if that's causing it and the problem still happens and I have no idea why the place2 function would be causing the issue.

I have spent probably 3 hours in total looking for a solution and have not found one so I'm hoping that SO can help.

martineau
  • 119,623
  • 25
  • 170
  • 301
Carson P
  • 313
  • 3
  • 13

1 Answers1

3

When you execute place2(inp), you will assign out = inp. This is not a copy ! What you're doing is pointing out toward inp. So when you change out, you also change inp.

You should use deepcopy if you don't want to modify your inp variable.

import copy

def place2(inp):

  out = copy.deepcopy(inp) # This will do a copy instead of pointing. 

  for i in range(4):
    n = randint(0,1)
    if n == 1:
        out[i] = flip(out[i])

     # etc.

To be clearer, here's what happens without deepcopy :

a = [1,2]
b = a
b[0] = 10
print(a)  # [10,2]

with deepcopy:

a = [1,2]
b = copy.deepcopy(a)
b[0] = 10
print(a)  # [1,2]
madjaoue
  • 5,104
  • 2
  • 19
  • 31