1

Say I have an array in Python, e.g.:

my_array = np.array([10, -5, 4, ...])
my_indices = np.array([0, 3, 10, ...])

How can I efficiently get:

  1. The list of indices of my_array that are not in my_indices
  2. The list of elements in my_array not referenced by my_indices (trivial with 1, but perhaps there is a direct way)
2
  • The question is a bit unclear. It'd be more useful if you showed actual inputs and expected outputs for each question Commented Feb 11, 2013 at 1:08
  • Possible dupe? stackoverflow.com/questions/8741445/… -- This question asks for a little bit more than the previous one however ... Commented Feb 11, 2013 at 1:24

4 Answers 4

5

I might do it something like this:

>>> import numpy as np
>>> a = np.random.random(10)  # set up a random array to play with
>>> a
array([ 0.20291643,  0.89973074,  0.14291639,  0.53535553,  0.21801353,
        0.05582776,  0.64301145,  0.56081956,  0.85771335,  0.6032354 ])
>>>
>>> b = np.array([0,5,6,9])  # indices we *don't want*
>>> mask = np.ones(a.shape,dtype=bool)
>>> mask[b] = False          # Converted to a mask array of indices we *do want*
>>> mask
array([False,  True,  True,  True,  True, False, False,  True,  True, False], dtype=bool)
>>>
>>> np.arange(a.shape[0])[mask]  #This gets you the indices that aren't in your original
array([1, 2, 3, 4, 7, 8])
>>> a[mask]  #This gets you the elements not in your original.
array([ 0.89973074,  0.14291639,  0.53535553,  0.21801353,  0.56081956,
        0.85771335])
Sign up to request clarification or add additional context in comments.

2 Comments

My inuition tells me that this is the most efficient of all answers (I would guess that indexing is "more vectorized" than list comprehensions). What do you think?
@user273158 -- There may be a better numpy way to do it still. Really though, you'd need to timeit to know for sure what you're best option is.
2

For part 1, you can just use the difference between the two sets using Python's built in set class.

my_array = [1,2,3,4]
my_indices = [3,4,5]

print list(set(my_array) - set(my_indices))

Will output: [1, 2].


EDIT

In order to return the list of indices in my_array that are not in my_indices, you could use list comprehension:

my_array = [1,2,3,4]
my_indices = [0,3]

print [x for x in range(len(my_array)) if x not in my_indices]

Which can also be expressed as:

temp = []
for x in range(len(my_array)):
  if x not in my_indices:
    temp.append(x) 

This will return the indices [1,2].

In you wanted to get the list of elements, then you can modify the statement to be:

print [my_array[x] for x in range(len(my_array)) if x not in my_indices]

Which will output [2,3].

2 Comments

Thanks, but in my first question I am looking for indices of my_array, not its elements, and in my second question I am looking for elements not referenced by my_indices. Your answer, while admittedly solves another problem, does not address these questions.
Ah okay. I think I understand. I've modified my answer.
1

You can use list comprehensions

array_len = len(my_array)
missing_indices = [i for i in my_indices
                   if i < 0 or i >= array_len]
elems = [my_array[i] for i in missing_indices]

Comments

1

For the first question:

my_indices_set = set(my_indices)
[i for i, x in enumerate(my_array) if i not in my_indices]

For the second question:

[x for x in my_array if x not in my_indices_set]

It's more efficient if we use sets, but then there's the cost of creating the sets in the first place

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.