gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
for np.where in numpy, what if I have two conditions?
- User
for np.where in numpy, what if I have two conditions?
- Assistant
If you have two conditions, you can use the logical operators "&" (and) or "|" (or) to combine the conditions. For example, to find the indices where an array is both greater than 5 and less than 10, you can use the following code:
codeimport numpy as np a = np.array([2, 6, 8, 10, 3, 12, 5]) indices = np.where((a > 5) & (a < 10)) print(indices)This will output:
code(array([1, 2]),)which is a tuple of arrays containing the indices where the condition is true.