# python numpy.where（）如何工作？

` `>>> x = np.arange(9.).reshape(3, 3) >>> np.where( x > 5 ) (array([2, 2, 2]), array([0, 1, 2]))` `

### 2 Solutions collect form web for “python numpy.where（）如何工作？”

numpy数组上的任何types的逻辑操作都会返回一个布尔数组。 （即`__gt__``__lt__`等全部返回布尔数组，其中给定的条件为真）。

` `x = np.arange(9).reshape(3,3) print x > 5` `

` `array([[False, False, False], [False, False, False], [ True, True, True]], dtype=bool)` `

` `>>> a = np.arange(100) >>> np.where(a > 30) (array([31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]),) >>> np.where(a == 90) (array([90]),) a = a*40 >>> np.where(a > 1000) (array([26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]),) >>> a[25] 1000 >>> a[26] 1040` `

` `a > 1000 array([False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True`, True, True, True, True, True, True, True, True, True], dtype=bool)`` `

` `>>> class demo(object): def __gt__(self, item): print item >>> a = demo() >>> a > 4 4` `