import numpy as np x = [1,2,3,4,5,6] x = np.array(x) y = x y[0] = 100 print(x) z = np.array(x) z[:] = x[:] z[0] = 1 print(x)
[100 2 3 4 5 6]
[100 2 3 4 5 6]
直接y=x相当于创建了个指针,两者地址都是一样的,改变其中一个另一个也会改变
而用[:]是创建了个新的地址,两者无关系
详见 https://stackoverflow.com/questions/20586230/what-is-the-difference-between-a-x-and-a-x-in-python
z[:] = 1 表示的是list中每个元素都是1