![]() |
VOOZH | about |
In this article, we are going to find the index of the elements present in a Numpy array.
where() method is used to specify the index of a particular element specified in the condition.
Syntax: numpy.where(condition[, x, y])
Here, we find all the indexes of 3 and the index of the first occurrence of 3, we get an array as output and it shows all the indexes where 3 is present.
Output:
All index value of 3 is: [2 7]
First index value of 3 is: 2
Here, we are printing the index number of all values present in the values array.
Output:
index of first occurrence of each value: [1 2 9]Get the index of elements with a value less than 20 and greater than 12
Output:
Index of elements with value less than 20 and greater than 12 are:
(array([ 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15], dtype=int64),)
Create a NumPy array and iterate over the array to compare the element in the array with the given array. If the element matches print the index.
Output:
element index : 5It is usually used to find the first occurrence of the element in the given numpy array.
Output:
(6,)Here we are using the enumerate function and then checking the value with the target value.
Output:
6