3

I have the numpy array:

a = np.array([[ 255,255,255],
              [ 255,2,255],
              [ 3,123,23],
              [ 255,255,255],
              [ 0, 255, 3]])

And I want to delete all the elements with [ 255,255,255], the result should be:

[[ 255,2,255],
 [ 3,123,23],
 [ 0, 255, 3]])

I tried with:

import numpy as np
a = np.array([[ 255,255,255],
              [ 255,2,255],
              [ 3,123,23],
              [ 255,255,255],
              [ 0, 255, 3]])

np.delete(a, [255,255,255])

but nothing happens.

2 Answers 2

4

You can do this:

np.array([x for x in a if np.any(x != 255)])

which gives:

array([[255,   2, 255],
       [  3, 123,  23],
       [  0, 255,   3]])

Edit: To avoid list comprehensions -

np.delete(a, np.where((a == 255).all(axis=1)), axis=0)
Sign up to request clarification or add additional context in comments.

Comments

3

here is a fast vectorized way to do it

a[(a!=255).any(axis=1),:]
Out[136]: 
array([[255,   2, 255],
       [  3, 123,  23],
       [  0, 255,   3]])

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.