python中具有相同函数的数组存在问题

python中具有相同函数的数组存在问题,python,arrays,Python,Arrays,所以我有一个非常奇怪的场景,我简化了代码,但是 同样的问题 def write_to_bf_of_lpr(bf_source, bf_destination, lpr_index, start, length): for x in range(length): bf_destination[lpr_index][start + x] = bf_source[start + x] source = ['a','b','c','d','e'] destination =

所以我有一个非常奇怪的场景,我简化了代码,但是 同样的问题

def write_to_bf_of_lpr(bf_source, bf_destination, lpr_index, start, length):
    for x in range(length):
        bf_destination[lpr_index][start + x] = bf_source[start + x]

source = ['a','b','c','d','e']

destination = [[0]*5]*3
dets2=[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

for x in range(1):
    write_to_bf_of_lpr(bf_source=source,bf_destination=dets2,lpr_index=x,start=1,length=2)

for x in range(1):
    write_to_bf_of_lpr(bf_source=source,bf_destination=destination,lpr_index=x,start=1,length=2)
这段代码很容易理解,我希望每次(或迭代)只更改特定的数组

现在:当我用支持的版本编写时:
destination=[[0]*5]*3
它在一次迭代中改变所有数组

当我写长版本(这不是最好的)时,我得到了正确的版本

您可以看到
dest2
是正确答案,而
destination
是错误答案

有趣的是,我只是复制了短版本的值。。。结果不同

是Pycharm bug、python还是我遗漏了什么? 请参阅输出的屏幕截图


这是因为,当您以现有方式定义数组时,基本上是在创建引用的副本,该引用指向
[0]*5
,这意味着如果更改一个,它们都会更改。在使用numpy时,您可以做您想做的事情,这样当您更改一个索引时,只有该索引会更改

import numpy as np

destination = np.zeros((3, 5))
您也可以在不使用numpy的情况下执行此操作,如下所示:


destination=[[0]*5用于范围内(3)]

谢谢,谢谢!