views:

95

answers:

5

I'm pretty new in Python (and programming as a whole). I'm pretty sure the answer to this is obvious, but I really don't know what to do.

def do_play(value, slot, board):
    temp=board
    (i,j) = slot
    temp[i][j] = value
    return temp

board is a list of lists. value is an integer. slot is and integer tuple.

What I am trying to do here is to

  • feed the function board
  • copy board to a new list called temp
  • insert a new value in a specific location in temp
  • return temp, leaving board unchanged

When I run this is the shell, both the the original list (board) and the new list (temp) change. = \

Any help would be appreciated.

+2  A: 

Use copy.deepcopy() to copy the object.

Ignacio Vazquez-Abrams
+1 for linking to standard library docs.
Wogan
+5  A: 

Are you trying to copy board?

temp = board[:]

Or maybe this to copy the structure.

temp = [ r[:] for r in board ]
S.Lott
This will only copy the top level. It appears that `board` is a list of lists based on the indexing performed on `temp` a few lines further down.
Ignacio Vazquez-Abrams
+10  A: 

temp=board does not make a new board. It makes the temp variable reference the very same list as board. So changing temp[i][j] changes board[i][j] too.

To make a copy, use

import copy
temp=copy.deepcopy(board)

Note that temp=board[:] makes temp refer to a new list (different than board, but the contents (that is, the lists within the list) are still the same:

In [158]: board=[[1,2],[3,4]]    
In [159]: temp=board[:]    

Modifying temp modifies board too:

In [161]: temp[1][0]=100    
In [162]: temp
Out[162]: [[1, 2], [100, 4]]    
In [163]: board
Out[163]: [[1, 2], [100, 4]]

id shows the object's memory address. This shows temp and board are different lists:

In [172]: id(temp)
Out[172]: 176446508

In [173]: id(board)
Out[173]: 178068780   # The ids don't match

But this shows the second list inside temp is the very same list inside board:

In [174]: id(temp[1])
Out[174]: 178827948

In [175]: id(board[1])
Out[175]: 178827948    # The ids are the same

But if you use copy.deepcopy, then the lists within the list also get copied, which is what you need if modifying temp is to leave board unchanged:

In [164]: import copy    
In [165]: board=[[1,2],[3,4]]    
In [166]: temp=copy.deepcopy(board)    
In [167]: temp[1][0]=100    
In [168]: temp
Out[168]: [[1, 2], [100, 4]]    
In [169]: board
Out[169]: [[1, 2], [3, 4]]
unutbu
A: 
temp=board

This doesn't copy list, it just makes one more reference to the same object. Use temp = board[:] instead.

Andrei
+2  A: 

Here temp is a reference to board, a shallow copy. I usually like to import the copy module (import copy) and use copy.deepcopy which makes temp separate from board. You'd call it something like this:

import copy
temp = copy.deepcopy(board)

Otherwise, you can just make a slice of board (which also makes a deep copy). I believe this should work, but I haven't tried it on a list of lists. You'd call it as so:

temp = board[:]
jlv
`[:]` operator does not make a deep copy. you had it right with your first option.
aaronasterling
Thanks! I was very much guessing with the slicing.
jlv