Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何添加列表中的元素_Python_Arrays_Numpy_Array Broadcasting - Fatal编程技术网

Python 如何添加列表中的元素

Python 如何添加列表中的元素,python,arrays,numpy,array-broadcasting,Python,Arrays,Numpy,Array Broadcasting,我有一个列表(Y),其中包含不同长度的numpy数组。该列表包含50多个元素(我刚刚测试了5个)。列表的形状是5,我可以打印每个元素的形状,如下所示,输出在注释中 print(Y.shape) #(5,) print(Y[0].shape) #(600, 2) print(Y[1].shape) #(250, 2) print(Y[2].shape) #(300, 2) print(Y[3].shape) #(200, 2) print(Y[4].shape) #(100, 2) 列表中的每个

我有一个列表(Y),其中包含不同长度的numpy数组。该列表包含50多个元素(我刚刚测试了5个)。列表的形状是5,我可以打印每个元素的形状,如下所示,输出在注释中

print(Y.shape) #(5,)
print(Y[0].shape) #(600, 2)
print(Y[1].shape) #(250, 2)
print(Y[2].shape) #(300, 2)
print(Y[3].shape) #(200, 2)
print(Y[4].shape) #(100, 2)
列表中的每个元素都有不同的长度(600250300200100),但都有[1 0]或[0 1]维度。我想添加这些元素并作为

(1450,2)

我试过了

Y=np.和(Y,轴=0)

它给出了一个广播错误ValueError:操作数不能与形状(600,2)(250,2)一起广播,我知道这需要(600,2)(600,2)或(250,2)(250,2),但我想将600和250相加

同样的函数适用于[80,20,30]和[40,20,30]这样的三维数组,我得到的输出为[120,20,30]

如何添加/求和这些元素?

使用:

例如:

import numpy as np

Y1 = np.ones((100, 2))
Y2 = np.ones((200, 2))
Y3 = np.ones((300, 2))

np.concatenate([Y1, Y2, Y3], axis=0).shape   # (600, 2)

AttributeError:“list”对象没有属性“shape”
。所以我怀疑Y是一个列表。如果Y是一个列表,正如OP所说的,那么你理解列表的意义是什么[arr for arr in Y]?Y不能是np.array,因为Y[0]和Y[1]的形状不同。所以Y很可能是一个列表,OP用Y.shape代替len(Y)。我认为列表理解是无关的,并且有点误导。但是,是的,无论哪种方式,只要使用
Y
。所以我会删除我的评论,我已经更新了答案。感谢您的反馈:)
import numpy as np

Y1 = np.ones((100, 2))
Y2 = np.ones((200, 2))
Y3 = np.ones((300, 2))

np.concatenate([Y1, Y2, Y3], axis=0).shape   # (600, 2)