Depending on the user input, I have different conditions to check for in my array. Assume there are a maximum of 3 conditions:
a must be 1
b must be positive
c must be True
Only where these three conditions evaluate as True, the array shall be processed:
myArr = np.random.rand(5)
a = np.array([1, 1, 1, 0, 1])
b = np.array([4, 3, -8, 7, 6])
c = np.array([True, False, True, True, True])
valid_indices = np.where((a == 1) & (b > 0) & (c == True))
>> valid_indices
>> Out: (array([0, 4], dtype=int64),)
Not knowing beforehand which of these conditions will be provided, I would have to check like this:
if a and not b and not c:
valid_indices = np.where(a == 1)
elif a and not b and c:
valid_indices = np.where((a == 1) & (c == True))
elif a and b and not c:
valid_indices = np.where((a == 1) & (b > 0))
elif not a and b and c:
valid_indices = np.where((b > 0) & (c == True))
elif not a and not b and c:
valid_indices = np.where(c == True)
elif not a and b and not c:
valid_indices = np.where((b > 0))
God forbid I add another condition. Things are getting really messy. I am looking for a way to dynamically add to the condition as if it were just a regular string or formatter. Is that possible?