Python - 函数有一个列表作为参数。如何在不更改第一个列表的情况下返回另一个列表?

发布于 2024-09-28 12:24:59 字数 436 浏览 10 评论 0原文

我对 Python(以及整个编程)还很陌生。我很确定这个问题的答案是显而易见的,但我真的不知道该怎么办。

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

board 是一个列表的列表。值是一个整数。槽是和整数元组。

我在这里想做的是将

  • 功能板
  • 复制板提供给一个名为 temp 的新列表,
  • 在 temp 的特定位置插入一个新值
  • 返回 temp,保持板不变

当我运行这是 shell 时,原始列表都是原始列表(board) 和新列表 (temp) 发生变化。 = \

任何帮助将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

魄砕の薆 2024-10-05 12:24:59

temp=board 不会制作新板。它使 temp 变量引用与 board 完全相同的列表。因此,更改 temp[i][j] 也会更改 board[i][j]

要制作副本,请使用

import copy
temp=copy.deepcopy(board)

注意 temp=board[:] 使 temp 引用新列表(与 board 不同,但内容(即列表中的列表)仍然相同:

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

修改 temp 也会修改 board

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 显示对象的内存地址。显示 tempboard 是不同的列表:

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

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

但这显示 temp 内的第二个列表与 board 内的列表完全相同>:

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

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

但是如果你使用copy.deepcopy,那么列表中的列表也会被复制,如果修改temp是为了离开board<,这就是你所需要的/代码> 不变:

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

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]]
ㄟ。诗瑗 2024-10-05 12:24:59

您是否正在尝试复制board

temp = board[:]

或者也许这是为了复制结构。

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

Are you trying to copy board?

temp = board[:]

Or maybe this to copy the structure.

temp = [ r[:] for r in board ]
微暖i 2024-10-05 12:24:59

使用 copy.deepcopy() 进行复制物体。

Use copy.deepcopy() to copy the object.

枯寂 2024-10-05 12:24:59

这里 temp 是对 board 的引用,一个浅拷贝。我通常喜欢导入复制模块(import copy)并使用copy.deepcopy,这使得temp与板分离。你可以这样称呼它:

import copy
temp = copy.deepcopy(board)

否则,你可以只制作一块板(这也可以制作一个深层副本)。我相信这应该可行,但我还没有在列表列表上尝试过。你可以这样称呼它:

temp = board[:]

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[:]
探春 2024-10-05 12:24:59
temp=board

这不会复制列表,它只是对同一对象再添加一个引用。请改用 temp = board[:]

temp=board

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文