List数据的复制
Numpy数据的复制
和list数据不同,即使是第一层,使用切片等方式的复制均是浅拷贝,也就是会相互影响:
old = np.array([1,2,3,4])
new = old[:]
new[0] = 0
'''
--------------------
Before:
old: array([1, 2, 3, 4])
new: array([1, 2, 3, 4])
After:
old: array([0, 2, 3, 4])
new: array([0, 2, 3, 4])
--------------------
'''
另外注意,只要old或new经过reshape或者concatenate等操作使维度发生变化后,两者相互就不再影响:
old = np.concatenate([old, [5]])
'''
--------------------
old: array([0, 2, 3, 4, 5])
new: array([0, 2, 3, 4])
--------------------
'''
old[0] = 1
'''
--------------------
old: array([1, 2, 3, 4, 5])
new: array([0, 2, 3, 4])
--------------------
'''