开发者

Python - Function has a list as argument. How to return another list without changing the first?

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 开发者_C百科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.


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]]


Are you trying to copy board?

temp = board[:]

Or maybe this to copy the structure.

temp = [ r[:] for r in board ]


Use copy.deepcopy() to copy the object.


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[:]


temp=board

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜