Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete some array elements from numpy array

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.

like image 904
ƒernando Valle Avatar asked Aug 14 '26 02:08

ƒernando Valle


1 Answers

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)
like image 91
CDJB Avatar answered Aug 16 '26 16:08

CDJB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!