當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。