2 indexing numpy multiple-conditions python-3.x
我可以numpy.where()在一个条件下工作,但不能在两个条件下工作。
对于一种情况:
import numpy as np
a = np.array([1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1, 1, 1, 2, 4, 5])
i, = np.where(a < 2)
print(i)
>> [ 0 5 8 10 11 12] ## indices where a[i] = 1
Run Code Online (Sandbox Code Playgroud)
对于两个条件:
# condition = (a > 1 and a < 3)
# i, = np.where(condition)
i, = np.where(a > 1 and a < 3)
print(i)
>> ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Run Code Online (Sandbox Code Playgroud)
我继续阅读a.any()并a.all() 在另一个 SO post 中阅读,但这对我的目的不起作用,因为我想要所有符合条件的索引而不是单个布尔值。
有没有办法适应这两种情况?