当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python Numpy ndarray.item()用法及代码示例


借助numpy.ndarray.item()方法,我们可以获取在numpy数组上给定索引处找到的数据元素。请记住,我们可以将索引作为一维参数,也可以是二维。

参数:
*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.

返回: 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


相关用法


注:本文由纯净天空筛选整理自Jitender_1998大神的英文原创作品 Python | Numpy ndarray.item()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。