numpy.where函数可以用于多个特定值吗?
我可以指定一个特定值:
>>> x = numpy.arange(5)
>>> numpy.where(x == 2)[0][0]
2
但我想做类似以下的事情。它当然会出错。
>>> numpy.where(x in [3,4])[0][0]
[3,4]
有没有办法在不迭代列表并组合生成的数组的情况下执行此操作?
编辑:我还有一系列未知长度和未知值的列表,因此我无法轻松形成np.where()的参数来搜索多个项目。传递清单要容易得多。
答案 0 :(得分:5)
您可以将AnyObject
功能与numpy.in1d
:
numpy.where
答案 1 :(得分:1)
我猜row[j*n+i]
可能对您有所帮助,而不是:
np.ind1d
答案 2 :(得分:1)
如果您只需要检查几个值,您可以:
import numpy as np
x = np.arange(4)
ret_arr = np.where([x == 1, x == 2, x == 4, x == 0])[1]
print "Ret arr = ",ret_arr
输出:
Ret arr = [1 2 0]