0

I want to be able to reset a nested list of Boolean values lists to a particular configuration once a certain condition has been reached inside a for loop.

So at the start of my program I define the list as follows:

g = [[True,True,False,False,False],[True,False,False,False,False],[False,False,False,False,False],[False,False,False,False,False],[False,False,False,False,False]]

Then I have a for loop:

for i in range(10):
    row = random.randrange(5)
    col = random.randrange(5)
    print str(col) + "\t" + str(row)
    gridC = doMove(gridC,col,row)
    s += str(col + 1) + "\t" + str(row + 1) + "\n"
    for i in gridC:
        print i
    if isFound(gridC[:]):
        found = True
        break
    else:
        s = ""
        gridC = g[:]

The loop terminates if all values in gridC are false. However when I try to reset gridC to g[:], it retains the memory of being changed in previous iterations of the loop. I can't work out any way of reliably resetting it to the correct configuration each time.

Edit Note: I am aware of the thread that explains copying a list but as you can see at the bottom of my for loop I have tried this with gridC = g[:]. The problem still persists and I cannot work out why.

  • `g[:]` is a *shallow* copy. – user2357112 Mar 28 '18 at 22:12
  • You can read the previous thread in more detail. It will explain that gridC=g[:] method of copying a list is, as the user above stated, only a shallow copy. It copies only the top layer. Since you have a list of lists, the internal lists are not copied. You can use the slicing method only for list of immutable objects. You might try copy.deepcopy() from the copy module. – enumaris Mar 28 '18 at 22:19

0 Answers0