1

I have an array of floats. I want to select its values based on multiple conditions:

import numpy as np
stamps = np.linspace(1., 100., 1e3)  
selected_stamps = stamps[((stamps > 2.)& (stamps < 10) & (stamps > 20.)& (stamps < 31) & (stamps > 80.)& (stamps < 95) )] # select only values within 2-10, 20-31, 80-95

How do I do that?

3
  • stackoverflow.com/questions/40677652/… Commented Apr 23, 2020 at 19:08
  • You can use np.all Commented Apr 23, 2020 at 19:09
  • Is the posted solution working? If so, what improvements are you looking for? Commented Apr 23, 2020 at 19:12

1 Answer 1

1

If you use only and operators as you did you will get an empty collection. You have 3 ranges, so between each range you need an or operator:

selected_stamps = stamps[(
    ((stamps > 2.) & (stamps < 10)) | 
    ((stamps > 20.)& (stamps < 31)) | 
    ((stamps > 80.)& (stamps < 95)) )]
Sign up to request clarification or add additional context in comments.

Comments

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.