微信公众号:yale记 关注可了解更多的教程问题或建议,请公众号留言。
今天我们学习NumPy数组的深拷贝与浅拷贝以及数组的属性使用。我们接着使用Jupyter Notebook实现所有的代码演示,接下来开始:
以下为在Jupyter Notebook中的执行过程:
代码过程:
# # NumPy
# ## 深拷贝与浅拷贝学习
import numpy as np
x = np.array([-45, -31, -12, 0, 2, 25, 51, 99])
y = x
# ### 查看素组是否相同
# 引用相同
x is y
id(x)
id(y)
x == y
y[4] = 1010
y
x
tree_house = np.array([-45, -31, -12, 0, 2, 25, 51, 99])
tree_house == y
id(tree_house)
id(x)
tree_house[0] = 214
tree_house
x
tree_house == x
tree_house is x
# ## 浅拷贝
farm_house = tree_house.view()
farm_house.shape = (2, 4)
tree_house
farm_house
tree_house[3] = -111
farm_house
# ## 深拷贝
dog_house = np.copy(tree_house)
dog_house[0] = -121
dog_house
tree_house