Numpy where returns empty array

点点圈 提交于 2020-08-07 09:47:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!