脚本无缘无故地更改变量的值

时间:2018-07-31 17:54:25

标签: python loops variables

运行脚本的此特定块时,record[r]会更改其值。这是我打印出来的几行:

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

代码:

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

它也毫无价值,我永远不会打印出GETTING PAST WHILE,所以我知道罪魁祸首一定是在while循环中。

record[r]while循环中应该是静态的,我一生无法解释为什么不是这样。我已经隔离出validate函数来查看是否是引起该问题的原因,并且问题仍然存在,并且我不知道为什么place2函数会导致该问题。

我总共花了大约3个小时来寻找解决方案,但没有找到解决方案,因此我希望SO能够提供帮助。

1 个答案:

答案 0 :(得分:3)

执行place2(inp)时,将分配out = inp。这不是副本! 您正在做的是将out指向inp。因此,当您更改out时,您也会更改inp

如果您不想修改deepcopy变量,则应使用inp

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.

更清楚地说,这是在没有Deepcopy的情况下发生的事情:

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

具有Deepcopy:

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