I have an array
JavaScript
x
2
1
a=[1,2,3,4,5,6,7,8,9]
2
and I want to find the indices of the element s that meet two conditions i.e.
JavaScript
1
4
1
a>3 and a<8
2
ans=[3,4,5,6]
3
a[ans]=[4,5,6,7]
4
I can use numpy.nonzero(a>3)
or numpy.nonzero(a<8)
but not
numpy.nonzero(a>3 and a<8)
which gives the error:
JavaScript
1
3
1
ValueError: The truth value of an array with more than one element is
2
ambiguous. Use a.any() or a.all()
3
When I try to use any
or all
I get the same error.
Is it possible to combine two conditional tests to get the ans?
Advertisement
Answer
JavaScript
1
2
1
numpy.nonzero((a > 3) & (a < 8))
2
& does an element-wise boolean and.