hm8
hm8

Reputation: 1513

Insert 2D array into 3D array in python

Lets say I make the following arrays:

>>> a = zeros((2,2,2))
>>> b = ones((2,2))

How can I insert b into the middle of a? So my resulting array will look like

array([[[ 0.,  0.],
        [ 0.,  0.]],

       [[ 1.,  1.],
        [ 1.,  1.]],

       [[ 0.,  0.],
        [ 0.,  0.]]])

Upvotes: 2

Views: 5011

Answers (3)

Mohsen Hrt
Mohsen Hrt

Reputation: 303

A way to show same as your result is using tuple. code:

b = numpy.ones((2,2))
a = numpy.zeros((2, 2))
tpl = a,
tpl = b,
tpl = a,

So you can consider it as 3dArray and also you can use for loop to add multi array into it.Hope be helpfull.

Upvotes: 0

James Carter
James Carter

Reputation: 833

a=np.zeros((3,2,2))
b=np.ones((2,2))

a[1]=b*1

That will produce your output.

Upvotes: -1

llllllllll
llllllllll

Reputation: 16404

You can use numpy.insert():

a = np.insert(a, 1, b, axis=0)

Upvotes: 3

Related Questions