Reputation: 139
I have two arrays, array A and B as:
import numpy as np
A = np.array(['A', 'B', 'C', 'D', 'E'])
B = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]])
which I want to be mixed so that array B is placed in between A to give me an array C of the form:
C = [[ 'A', '1', 'B', '2', 'C', '3', 'D', '4', 'E', '5'],
[ 'A', '6', 'B', '7', 'C', '8', 'D', '9', 'E', '10'],
[ 'A', '11', 'B', '12', 'C', '13', 'D', '14', 'E', '15']]
Upvotes: 2
Views: 65
Reputation: 9726
You can use a combination of reshape
(to expose the target axis) and concatenate
(to join the arrays along this axis), with reshape
ing back to the desired form:
import numpy as np
A = np.array(['A', 'B', 'C', 'D', 'E'])
B = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]])
AA = np.tile(A, 3).reshape(3, 5, 1)
BB = B.reshape(3, 5, 1)
C = np.concatenate([AA, BB], axis=2).reshape(3, 10)
print(C)
Upvotes: 2