简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM