Python| numpy ndarray.item()
借助numpy.ndarray.item()
方法,我们可以获取在 numpy 数组的给定索引处找到的数据元素。请记住,我们可以将索引作为一维参数,也可以是二维的。
Parameters:
*args : Arguments (variable number and type)
-> none: This argument only works when size of an array is 1.
-> int_type: This argument is interpreted as a flat index into the array, specifying which element to return.
-> tuple of int_types: This argument is interpreted as a two dimensional array, by specifying which element to return.
Returns: Copy of an Item
示例 #1:
在这个例子中,我们可以看到通过在ndarray.item()
方法中指定参数,我们可以拥有该元素是否存在于该索引上。
# 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.item() method
print(gfg.item(2))
输出:
3
示例 #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.item() method
print(gfg.item((1, 2)))
输出:
4