0

I'm trying to create a code to simulate the spread of something, via a 2D list of nxn structure. My issue is this: when I create a temp of my original list via temp = [*board], board[:], etc. it nonetheless updates both lists and instead of returning,

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 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 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 1 0 0 0 0 0 0 0 
0 0 0 0 0 0 1 1 1 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 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 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

returns

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 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 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 1 1 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1 1 1 1

my code is here:

def spread(board, iterations, size):
    temp = board[:]
    for iteration in range(iterations):
        for x in range(size):
            for y in range(size):
                if board[x][y] == 1:
                    if x+1 < size:
                        temp[x+1][y] = 1
                    if x-1 >= 0:
                        temp[x-1][y] = 1
                    if y+1 < size:
                        temp[x][y+1] = 1
                    if y-1 >= 0:
                        temp[x][y-1] = 1
        board = temp[:]
return board

and I called it via

new_board = spread(my_board, 1, 15)
  • 3
    `[:]` only does a shallow copy of the list. You probably need a deep copy (`import copy; temp = copy.deepcopy(board)`) – Wondercricket Jan 13 '22 at 19:16
  • Does this answer your question? [List changes unexpectedly after assignment. Why is this and how can I prevent it?](https://stackoverflow.com/questions/2612802/list-changes-unexpectedly-after-assignment-why-is-this-and-how-can-i-prevent-it) – Bharel Jan 13 '22 at 19:18
  • @Wondercricket This works! Thank you :) – Blake B Jan 13 '22 at 19:22

1 Answers1

0

This is programming 101. Remember, lists are stored in heap, with pointers to them.

So really the variable board points to the place in heap where that array is stored. When you assign temp to board, what you are doing is creating a new pointer which points to that same array. I suggest taking a look at this using python tutor: https://pythontutor.com/visualize.html#mode=edit

For example:

b = [1,2,3,4,5]
a = b 
a[0] = 2
print(b)

will output

[2,2,3,4,5]

Try it out in python tutor and you'll see what's happening!

To solve your problem, create a deep copy

def deep_copy(board):
   temp = []
   for i in range(len(board)):
       row_copy = []
       for j in range(len(board[0])):
           row_copy.append(board[i][j])
       temp.append(row_copy)
   return temp
dvr
  • 370
  • 1
  • 9