Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在多个条件下使用np.where_Python_Numpy_Np - Fatal编程技术网

Python 如何在多个条件下使用np.where

Python 如何在多个条件下使用np.where,python,numpy,np,Python,Numpy,Np,在使用np.where()获取满足这些条件的所有的数组元素的索引时,如何指定多个条件 a = np.array([1, 2, 3, 4, 5, 6]) print(np.where(a > 2 and a < 5)) 我得到了索引[2,3,4,5],但现在我只想得到[2,3]。必须使用位运算符,&表示and,表示or,依此类推 以你为例, a = np.array([1, 2, 3, 4, 5, 6]) np.where((a > 2) & (a < 5))

在使用np.where()获取满足这些条件的所有的数组元素的索引时,如何指定多个条件

a = np.array([1, 2, 3, 4, 5, 6]) 
print(np.where(a > 2 and a < 5))

我得到了索引[2,3,4,5],但现在我只想得到[2,3]。

必须使用位运算符,
&
表示and,
表示or,依此类推

以你为例,

a = np.array([1, 2, 3, 4, 5, 6])
np.where((a > 2) & (a < 5))

np.where((a>2)和(a<5))
你想要什么?是的,谢谢!为什么它不能与and一起工作?
运算符不能被重写,但是
&
可以,所以numpy就是这样做的。它只是没有机会定义自己的
,因为Python解释器不允许这样做。
a = np.array([1, 2, 3, 4, 5, 6])
np.where((a > 2) & (a < 5))
(array([2, 3], dtype=int64),)