0

Let's say I am manipulating a very large array of ints in numpy ( ). I want to filter it with a sublist of its values sublist. As the array is really large it looks like I need to be smart to do it in teh quickest way.

For instance:

my_array = N.random.randint(size=1e10)
sublist = [4,7,9]
#core where I extract the values of my_array equal to 4, 7 or 9

I've thought about:

cut = N.zeros((len(my_array)),dtype=bool)
for val in sublist:
    cut = cut | (my_array == val)
my_array = my_array[cut]

but it would have to parse the array len(sublist) amount of time.

Still manually:

cut = N.array([value in sublist for value in my_array])
my_array = my_array[cut]

but is there a more numpytonic way of doing so?

2
  • Is my_array sorted? Commented Jul 23, 2015 at 14:20
  • @Divakar: not in the most general case. Commented Jul 23, 2015 at 16:04

1 Answer 1

4

numpy.in1d does exactly this. Your code would look like:

cut = N.in1d(my_array, sublist)
my_array = my_array[cut]
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.