一种方法是使用numpy.hstack
with ravel
压平各种尺寸。
import numpy as np
l = (np.array([0.08]), np.array([ 0.56, -0.01, 0.46]),
np.array([[ 0.60], [0.07], [0.03]]), np.array([[0., 0., 0., 0.]]),
np.array([[0.]]))
res = np.hstack(i.ravel() for i in l)
array([ 0.08, 0.56, -0.01, 0.46, 0.6 , 0.07, 0.03, 0. , 0. ,
0. , 0. , 0. ])
或者,如果您想要一种功能性方法:
from operator import methodcaller
res = np.hstack(list(map(methodcaller('ravel'), l)))