How to insert numpy 1D array to numpy 3D array?

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?

Upvotes: 1

Views: 492

Answers (1)

Joe Iddon
Joe Iddon

Reputation: 20414

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]]]

Upvotes: 1

Related Questions