1

So I've got a 3-dimensional numpy array and I want to insert into it a 1-dimensional numpy array. How can I do it?
For example, this is my 3D array and I want to insert [2,2,2]

[[[1,1,1],
  [3,3,3],
  [4,4,4]],
 [[5,5,5],
  [6,6,6],
  [7,7,7]]]

so it looks like this:

[[[1,1,1],
  [2,2,2],
  [3,3,3],
  [4,4,4]],
 [[5,5,5],
  [6,6,6],
  [7,7,7]]]

How can I do it?

1
  • Something's missing - the row between the 5s and 6s. Commented Aug 7, 2019 at 10:30

1 Answer 1

1

You cannot do this with standard numpy arrays as they must remain rectangular. Potentially, you could create one with dtype=object, but this seems to me like you would lose the efficiency of numpy.

Maybe you are better off with regular lists?

l = [[[1,1,1],
      [3,3,3],
      [4,4,4]],
     [[5,5,5],
      [6,6,6],
      [7,7,7]]]
l[0].insert(1, [2,2,2])

which modifies l to:

l = [[[1,1,1],
      [2,2,2],
      [3,3,3],
      [4,4,4]],
     [[5,5,5],
      [6,6,6],
      [7,7,7]]]
Sign up to request clarification or add additional context in comments.

1 Comment

Nice idea! Thanks

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.