gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:36:03 26 4
gpt4 key购买 nike

在上一个问题中,我询问了有关 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 以通用方式执行此操作

最佳答案

您可以将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 ])

关于Python:将 3 个矩阵连接到一个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42490695/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com