numpy.ndarray.item#
method
- ndarray.item(*args)#
Copy an element of an array to a standard Python scalar and return it.
- Parameters:
- *argsArguments (variable number and type)
none: in this case, the method only works for arrayswith one element (a.size == 1), which element iscopied into a standard Python scalar object and returned.
int_type: this argument is interpreted as a flat index intothe array, specifying which element to copy and return.
tuple of int_types: functions as does a single int_type argument,except that the argument is interpreted as an nd-index into thearray.
- Returns:
- zStandard Python scalar object
A copy of the specified element of the array as a suitablePython scalar
Notes
When the data type ofa is longdouble or clongdouble, item() returnsa scalar array object because there is no available Python scalar thatwould not lose information. Void arrays return a buffer object for item(),unless fields are defined, in which case a tuple is returned.
item
is very similar to a[args], except, instead of an array scalar,a standard Python scalar is returned. This can be useful for speeding upaccess to elements of the array and doing arithmetic on elements of thearray using Python’s optimized math.Examples
>>>importnumpyasnp>>>np.random.seed(123)>>>x=np.random.randint(9,size=(3,3))>>>xarray([[2, 2, 6], [1, 3, 6], [1, 0, 1]])>>>x.item(3)1>>>x.item(7)0>>>x.item((0,1))2>>>x.item((2,2))1
For an array with object dtype, elements are returned as-is.
>>>a=np.array([np.int64(1)],dtype=object)>>>a.item()#return np.int64np.int64(1)