0

How come this be right?

X=np.array([range(1,12)])     

A=X>4
B=X<10
C=(X>4) | (X<10)
print (X[A])
print (X[B])
print (X[C])



[ 5  6  7  8  9 10 11]
[1 2 3 4 5 6 7 8 9]
[ 1  2  3  4  5  6  7  8  9 10 11]

9
  • 1
    What's not clear? The first array is every element more than 4 Commented Feb 9, 2020 at 7:10
  • docs.scipy.org/doc/numpy/user/… Commented Feb 9, 2020 at 7:11
  • How's variable C output right? I meant Commented Feb 9, 2020 at 7:11
  • Yes right... im dumb. Thanks Commented Feb 9, 2020 at 7:12
  • Because all elements are greater than 4 OR less than 10... Did you mean to use AND operator? Commented Feb 9, 2020 at 7:13

1 Answer 1

3

I'm going to guess that your concern is because you have every element in the final expression, simply because the first two are obvious (5..11 are all greater than four and 1..9 are all less than ten).

But the third one is also right since every element is either greater than four or less than ten. The numbers 1..9 are all less than ten so they're in. Similarly, 5..11 are all greater than four so they're in as well. The union of those two ranges is the entire set of values.

If you wanted the items that were between four and ten (exclusive at both ends), you should probably have used "and" instead of "or" (& instead of |):

import numpy as np
X=np.array([range(1,12)])     

A=X>4
B=X<10
C=(X>4) | (X<10)
D=(X>4) & (X<10)
E=(X<=4) | (X>=10)
print (X[A])
print (X[B])
print (X[C])
print (X[D])
print (X[E])

The output of that is:

[ 5  6  7  8  9 10 11]
[1 2 3 4 5 6 7 8 9]
[ 1  2  3  4  5  6  7  8  9 10 11]
[5 6 7 8 9]
[ 1  2  3  4 10 11]

Because you didn't specify what you wanted in the original question), I've also added the opposite operation (to get values not in that range). That's indicated by the E code.

Sign up to request clarification or add additional context in comments.

7 Comments

I thought that when C=(X>4) | (X<10) , one of the condition should be True.
@Cody, no its "one or the other, or both". You're thinking of exclusive or, XOR
@Cody, it's true if one or both are true (inclusive or). The operation for checking if only one is true is the exclusive or.
if i use & , that comes out [5 6 7 8 9]
@Cody: Ah, I see now. Unfortunately, that is a misunderstanding as to how it works. You may be better off thinking of it as a filter, with every element matching the expression being allowed through the filter. So, in the same way X>4 gives you everything higher than four, (X > 4) | (X < 10) gives you everything that matches that expression, which is everything that matches one or both of the subexpressions, which (in this case) is everything. You could also consider | being set union if you wish, it'll have the same result.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.