Python| Numpy ndarray.__copy__()
在Numpy ndarray.__copy__()
方法的帮助下,我们可以复制numpy array中存在的所有数据元素。如果您更改副本中的任何数据元素,它不会影响原始 numpy 数组。
Syntax : numpy.__copy__()
Return : Copy of all the data elements
示例 #1:
在这个例子中,我们可以看到在numpy.__copy__()
方法的帮助下,我们正在制作元素的副本。
# import the important module in python
import numpy as np
# make an array with numpy
gfg = np.array([1, 2, 3, 4, 5])
# applying ndarray.__copy__() method
geeks = gfg.__copy__()
print(geeks)
输出:
[1 2 3 4 5]
示例 #2:
# import the important module in python
import numpy as np
# make an array with numpy
gfg = np.array([[1, 2, 3, 4, 5],
[6, 5, 4, 3, 2]])
# applying ndarray.__copy__() method
geeks = gfg.__copy__()
# Change the data element
geeks[0][2] = 10
print(gfg, end ='\n\n')
print(geeks)
输出:
[[1 2 3 4 5]
[6 5 4 3 2]]
[[ 1 2 10 4 5]
[ 6 5 4 3 2]]