Yes, given an array, array, and a value, item to search for, you can use np.where as:
itemindex = numpy.where(array == item)
The result is a tuple with first all the row indices, then all the column indices.
For example, if an array is two dimensions and it contained your item at two locations then
array[itemindex[0][0]][itemindex[1][0]]
would be equal to your item and so would be:
array[itemindex[0][1]][itemindex[1][1]]
Answer from Alex on Stack OverflowYes, given an array, array, and a value, item to search for, you can use np.where as:
itemindex = numpy.where(array == item)
The result is a tuple with first all the row indices, then all the column indices.
For example, if an array is two dimensions and it contained your item at two locations then
array[itemindex[0][0]][itemindex[1][0]]
would be equal to your item and so would be:
array[itemindex[0][1]][itemindex[1][1]]
If you need the index of the first occurrence of only one value, you can use nonzero (or where, which amounts to the same thing in this case):
>>> t = array([1, 1, 1, 2, 2, 3, 8, 3, 8, 8])
>>> nonzero(t == 8)
(array([6, 8, 9]),)
>>> nonzero(t == 8)[0][0]
6
If you need the first index of each of many values, you could obviously do the same as above repeatedly, but there is a trick that may be faster. The following finds the indices of the first element of each subsequence:
>>> nonzero(r_[1, diff(t)[:-1]])
(array([0, 3, 5, 6, 7, 8]),)
Notice that it finds the beginning of both subsequence of 3s and both subsequences of 8s:
[1, 1, 1, 2, 2, 3, 8, 3, 8, 8]
So it's slightly different than finding the first occurrence of each value. In your program, you may be able to work with a sorted version of t to get what you want:
>>> st = sorted(t)
>>> nonzero(r_[1, diff(st)[:-1]])
(array([0, 3, 5, 7]),)
python - Index of element in NumPy array - Stack Overflow
efficiently finding the index of a value in a numpy array
How to find index of value in NumPy array? - Data Exploration & Visualization - Data Science Dojo Discussions
How can you find the index an element has in the NumPy array that a slice was made from?
Videos
Use np.where to get the indices where a given condition is True.
Examples:
For a 2D np.ndarray called a:
i, j = np.where(a == value) # when comparing arrays of integers
i, j = np.where(np.isclose(a, value)) # when comparing floating-point arrays
For a 1D array:
i, = np.where(a == value) # integers
i, = np.where(np.isclose(a, value)) # floating-point
Note that this also works for conditions like >=, <=, != and so forth...
You can also create a subclass of np.ndarray with an index() method:
class myarray(np.ndarray):
def __new__(cls, *args, **kwargs):
return np.array(*args, **kwargs).view(myarray)
def index(self, value):
return np.where(self == value)
Testing:
a = myarray([1,2,3,4,4,4,5,6,4,4,4])
a.index(4)
#(array([ 3, 4, 5, 8, 9, 10]),)
You can convert a numpy array to list and get its index .
for example:
tmp = [1,2,3,4,5] #python list
a = numpy.array(tmp) #numpy array
i = list(a).index(2) # i will return index of 2, which is 1
this is just what you wanted.
I have a numpy array that has unique values and is static, and I routinely want to find some index of a value. Is it a good idea to repeatedly use where for this? Is numpy sorting the values and storing a mapping of them to the indices behind the scene, or otherwise doing something smart to quickly find the index? If not, what would be a good way to implement finding the index of a value in a numpy array?
If I have a numpy array and a sub-array made from a slice, such as
import numpy as np a = np.array(['a','b','c','a','e','f']) b = a[2:4]
Is there a way that I can find the index n that an element of b, say b[1], can be referenced to using a[n]? I don't want an index of any 'a' element (such as a[0]) I want specifically the index that b[1] has (which is a[3])
In [1]: import numpy as np In [2]: a = np.array([0,0,0,0]) In [3]: b = np.array([1,0,0,0]) In [4]: c = np.where(a != b) In [5]: print(c) (array([0], dtype=int64),)
How do I get the value "0" out of the return of "c" ?
The best way would be to use np.argwhere:
tuple(np.argwhere(a == 0)[0])
If you want to use np.where, then you can do:
next(zip(*np.where(array == 0)))
That tuple of arrays you got were the indices for each axis. We can zip them together to get coordinate pairs. Then we call next on the zip iterator to get the first element from it, i.e. the coordinates of the first 0 found by np.where. You can test that these are the coordinates for the zeros by verifying that the when we use them as indexes for array it always outputs zero:
[array[x] for x in zip(*np.where(array == 0))]
Both of these approaches have the disadvantage of operating on the entire array, when they ought to stop after the first match. Stopping after the first match is a feature request that can be tracked here: https://github.com/numpy/numpy/issues/2269
The first element of the tuple that you got should be the index you are looking.