我有两个数组,数组A和B为:
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]])
我希望将其混合,以便将数组B放在A之间,以便为我提供以下形式的数组C:
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']]
答案 0 :(得分:2)
您可以使用reshape
(以显示目标轴)和concatenate
(沿此轴连接数组)的组合,reshape
返回所需的形式:
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)