我正在尝试找到一种解决方案,用于展平以下numpy阵列列表:
a = np.arange(9).reshape(3,3)
b = np.arange(25).reshape(5,5)
c = np.arange(4).reshape(2,2)
myarrs = [a,b,c]
d = np.arange(5*5*5).reshape(5,5,5)
myarrs2 = [a,b,c,d]
对于我的myarrs
,我目前正在使用:
res = np.hstack([np.hstack(i) for i in myarrs])
但我想知道是否有其他内置方法来执行此任务,特别是在具有不同形状的数组的情况下。 我看到了其他问题:Flattening a list of NumPy arrays?但它们通常指的是具有相同形状的数组
您可以尝试以下方法:
这应该比您的方法更快:
我知道您正在寻找} 或^{} 或^{} 一起使用:
numpy
唯一的解决方案。但是,如果允许,还有一种可能性是将more_itertools
与^{比较:
原始解决方案
来自norok2的解决方案(最快)
含有
more_itertools
+reshape(-1)
含
more_itertools
+ravel()
含
more_itertools
+flatten()
相关问题 更多 >
编程相关推荐