📜  Python| numpy ndarray.__array__()

📅  最后修改于: 2022-05-13 01:54:25.644000             🧑  作者: Mango

Python| numpy ndarray.__array__()

ndarray.__array__()方法的帮助下,我们可以通过将参数作为dtype来创建一个我们想要的新数组,如果我们更改任何一个,我们可以获得一个不会更改原始数组数据元素的数组副本新元素中的元素。

示例 #1:

在此示例中,我们可以看到我们仅使用ndarray.__array__()方法更改了新数组的dtype

# 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.__array__() method
geeks = gfg.__array__(float)
  
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.1, 2, 3.3, 4, 5],
                [6, 5.2, 4, 3, 2.2]])
        
# applying ndarray.__array__() method
geeks = gfg.__array__(int)
  
print(geeks)
输出:
[[1 2 3 4 5]
 [6 5 4 3 2]]