2

I have a 2D array like this:

 [array([71, 35, 44,  0])
 array([56, 55,  0])
 array([32, 90, 11])
 array([ 0,  3, 81,  9, 20])
 array([0, 0]) array([0, 0]) array([0, 0]) array([ 5, 89])]

and I want to remove [0, 0]

I try to

myarray = np.delete(myarray, np.where(myarray == [0, 0]), axis=0)

but it doesnt work.

How can I remove [0, 0] ?

3
  • Are you wanting to remove all elements from your list of arrays that are equal to array([0, 0])? Commented Sep 9, 2018 at 0:51
  • I would like to remove all array([0, 0]) Commented Sep 9, 2018 at 0:52
  • That's not a 2d array. It's a 1d array of objects (pointers), which happen to be arrays of varying length. You might has well think of it as a list of arrays. Commented Sep 9, 2018 at 2:35

2 Answers 2

3

Use a list comprehension with np.array_equal:

>>> [i for i in arr if not np.array_equal(i, [0,0])]

[array([71, 35, 44,  0]),
 array([56, 55,  0]),
 array([32, 90, 11]),
 array([ 0,  3, 81,  9, 20]),
 array([ 5, 89])]

However, it is best to not work with jagged numpy arrays, as numpy does not behave well with such arrays.

Sign up to request clarification or add additional context in comments.

2 Comments

I store them like this: np.array([np.array([1, 2, 3]), np.array([4, 5])]). I thought this would make an array of arrays of numpy type
Numpy doesn't handle jagged arrays either.
1

If you have numpy array to delete first row of a 2d array: insert axis=0 to remove rows and axis=1 to remove columns

array2d=np.delete(array2d, 0, axis=0)

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.