问题
I have an array e.g.
a = [5,1,3,0,2]
I apply the where function:
np.where(a == 2)
The output is an empty array
(array([], dtype=int64),)
I found kind of the same problem here, but in my case it really dosen't make any sense or dose it?
Btw. i'm on a Mac using Python 2.7.10
回答1:
You're passing a list to where()
function not a Numpy array. Use an array instead:
In [20]: a = np.array([5,1,3,0,2])
In [21]: np.where(a == 2)
Out[21]: (array([4]),)
Also as mentioned in comments, in this case the value of a == 2
is False
, and this is the value passed to where
. If a
is a numpy array, then the value of a == 2
is a numpy array of bools, and the where
function will give you the desire results.
来源:https://stackoverflow.com/questions/48369104/numpy-where-returns-empty-array