在Python中将一些数组合并为单个数组/列表 [英] Merging some array into single array/list in python
本文介绍了在Python中将一些数组合并为单个数组/列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何隐藏此数组(不同尺寸的numpy),
How to covert this array (different dimensions numpy),
l= (array([0.08]), array([[ 0.56, -0.01, 0.46]), array([[ 0.60], [0.07], [0.03]]), array([[0., 0., 0., 0.]]), array([[0.]]))
放入一维数组,
l= array([0.08, 0.56, -0.01, 0.60, 0.07, 0.03, 0., 0., 0., 0., 0.])
推荐答案
一种方法是使用 numpy .hstack
和 ravel
来展平各个尺寸。
One way is to use numpy.hstack
with ravel
to flatten the various dimensions.
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. ])
或者如果您想使用功能性方法:
Or if you want a functional approach:
from operator import methodcaller
res = np.hstack(list(map(methodcaller('ravel'), l)))
这篇关于在Python中将一些数组合并为单个数组/列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文