记录一下之前没有遇到过的问题。
1、首先key是一个numpy数组,但是这个赋值给key_new之后改变key_new 同样也会改变key。相当于是他们两个变量指向了同一块地址,所有改变一个另一个就会变。
key
Out[35]:
array([[ 1, 2, 3, 4],
[14, 6, 14, 14],
[13, 2, 1, 9],
[ 9, 12, 12, 6],
[14, 4, 4, 10]])
key_new = key
key_new[0][0]=3
key
Out[37]:
array([[ 3, 2, 3, 4],
[14, 6, 14, 14],
[13, 2, 1, 9],
[ 9, 12, 12, 6],
[14, 4, 4, 10]])
2、不仅仅是numpy的数组,一般的list也是一样。
key1=key.tolist()
key2=key1
key1
Out[43]: [[1, 2, 3, 4], [14, 6, 14, 14], [13, 2, 1, 9], [9, 12, 12, 6], [14, 4, 4, 10]]
key2
Out[44]: [[1, 2, 3, 4], [14, 6, 14, 14], [13, 2, 1, 9], [9, 12, 12, 6], [14, 4, 4, 10]]
key1[0][0]=3
key1
Out[46]: [[3, 2, 3, 4], [14, 6, 14, 14], [13, 2, 1, 9], [9, 12, 12, 6], [14, 4, 4, 10]]
key2
Out[47]: [[3, 2, 3, 4], [14, 6, 14, 14], [13, 2, 1, 9], [9, 12, 12, 6], [14, 4, 4, 10]]
3、遇到python里面的数组,记得用copy()函数复制值
key2=key1.copy()
key1
Out[56]: [2, 2, 3, 4]
key2
Out[57]: [2, 2, 3, 4]
key1[0]=1
key1
Out[59]: [1, 2, 3, 4]
key2
Out[60]: [2, 2, 3, 4]