Python:将3个矩阵连接到列表

时间:2017-02-27 16:23:00

标签: python arrays numpy

在之前的一个问题中,我询问了如何在python中连接3个数组

Python: Concatenate 3 arrays

我没有设法获得以下示例

假设我有3个阵列

a1=[
array([628.95, 632.012, 632.67, 270.70, 275.23, 275.20])
array([13.87, -24.55, 13.012, 17.61, -7.07, 2.29])
array([-4.56089042, -4.76182533, -5.84063203, -1.4404148, 23.11891765, 15.67678579])
]

a2=[
array([2353.12, 2357.60, 2360.42, 1718.49, 1703.65322389, 1718.781])
array([-31.07, 50.49, -32.94, 32.27, 20.404, 5.79])
array([-5.75, -4.70, -3.86, 30.25, 66.60, 64.626])
]


a3=[
array([2716.476, 2724.714, 2731.84, 1944.73, 1920.258, 1926.840])
array([-14.30, 20.78, -17.90, 44.94, -15.26, 19.888])
array([-13.959, -12.62, -10.84, 4.227, 45.744, 42.448])
]

我想获得以下输出:

r = a1[0] + a2[0] + a3[0] + a1[1] + a2[1] + a3[1] +a1[2] + a2[2] + a3[2] 

r = [628.95, 632.012, 632.67, 270.70, 275.23, 275.20, 2353.12, 2357.60, 2360.42, 1718.49, 1703.65322389, 1718.781, 2716.476, 2724.714, 2731.84, 1944.73, 1920.258, 1926.840,13.87, -24.55, 13.012, 17.61, -7.07, 2.29, -31.07, 50.49, -32.94, 32.27, 20.404, 5.79    .... ]     

使用numpy

以通用方式执行此操作的人员

1 个答案:

答案 0 :(得分:1)

您可以将zipnumpy.concatenate

一起使用
import numpy as np
a = np.concatenate([np.concatenate(x) for x in zip(a1, a2, a3)])
a

#array([  628.95      ,   632.012     ,   632.67      ,   270.7       ,
#         275.23      ,   275.2       ,  2353.12      ,  2357.6       ,
#        2360.42      ,  1718.49      ,  1703.65322389,  1718.781     ,
#        2716.476     ,  2724.714     ,  2731.84      ,  1944.73      ,
#        1920.258     ,  1926.84      ,    13.87      ,   -24.55      ,
#          13.012     ,    17.61      ,    -7.07      ,     2.29      ,
#         -31.07      ,    50.49      ,   -32.94      ,    32.27      ,
#          20.404     ,     5.79      ,   -14.3       ,    20.78      ,
#         -17.9       ,    44.94      ,   -15.26      ,    19.888     ,
#          -4.56089042,    -4.76182533,    -5.84063203,    -1.4404148 ,
#          23.11891765,    15.67678579,    -5.75      ,    -4.7       ,
#          -3.86      ,    30.25      ,    66.6       ,    64.626     ,
#         -13.959     ,   -12.62      ,   -10.84      ,     4.227     ,
#          45.744     ,    42.448     ])