<分区>
我正在尝试在 python 中复制一个 numpy 数组。 然后我想更改新数组的一些值,但是不知何故这也会更改原始数组?
为什么下面的代码不正确?
import numpy as np
a = np.array([1,1])
print("Array a:",a)
b = a
b[0] = a[0]*2
print("Array b after manipulation:", b)
print("Array a, after manipulating array b", a)
我能让它工作的唯一方法是列表理解。
import numpy as np
a = np.array([1,1])
print("Array a:",a)
b = [x for x in a]
b[0] = a[0]*2
print("Array b after manipulation:", b)
print("Array a, after manipulating array b", a)