0

I have a 2-dimensional array with each element being a list of valid values (pseudosyntax):

[ [1,2] [1,2]
  [1,2] [1,2] ]

so effectively it's a 3-dim array/list/matrix (whatever Python calls those.)

It is generated by

    import numpy as np
    grid = np.full((borderSize, borderSize, borderSize), range(1, borderSize + 1))

What is the best practice to remove a value from an array on the "deepest" level?

Pseudocode:

grid = [ [1,2] [1,2]
  [1,2] [1,2] ]

grid[0,1].remove(not index but value 2)

#result: grid = [ [1,2] [1]
                  [1,2] [1,2] ]

I did attempt every solution I could google, including numpy.delete() and array.remove(), any whatever that syntax is: arr = arr[ (arr >= 6) & (arr <= 10) ]. All the approaches seem to "flatten" the array/list/matrix, or throw cryptic-to-me errors ("can't broadcast array into shape")

Thanks!

1 Answer 1

1

Arrays don't really have such a thing as a "deepest" level, they have dimensions and each and every dimension has to be defined globally. You can change your array to a list and then remove the element like that:

grid = np.array([ [1,2], [1,2],
  [1,2], [1,2] ])

grid = grid.tolist()

del grid[1][1]

grid

result:

[[1, 2], [1], [1, 2], [1, 2]]

but you cannot do this on an array.

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

2 Comments

Thanks! Quick sanity check: I can't convert that list back to array, as not each "member" has the same number of elements - correct?
yup, arrays have to have dimensions defined. you can see your array dimension by typing grid.shape

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.