[英]Python: converting multi-dimensional numpy arrays into list of arrays
Suppose I have 2D array
, for simplicity, a=np.array([[1,2],[3,4]])
. 假设我有2D
array
,为简单起见, a=np.array([[1,2],[3,4]])
。 I want to convert it to list
of arrays, so that the result would be: 我想将其转换为数组
list
,这样结果将是:
b=[np.array([1,2]), np.array([3,4])]
I found out that there is np.ndarray.tolist()
function, but it converts ND array into nested list
. 我发现有
np.ndarray.tolist()
函数,但是它将ND数组转换为嵌套list
。 I could have done in a for
loop (using append
method), but it is not efficient/elegant. 我本可以在
for
循环中完成(使用append
方法),但这不是有效/优雅的方法。
In my real example I am working with 2D arrays of approximately 10000 x 50 elements and I want list
that contains 50 one dimensional arrays, each of the shape (10000,)
. 在我的真实示例中,我正在使用大约10000 x 50元素的2D数组,并且我想要包含50个一维数组的
list
,每个数组的形状为(10000,)
。
How about using list
: 如何使用
list
:
a=np.array([[1,2],[3,4]])
b = list(a)
Why don't you use list comprehension as follows without using any append
: 为什么不按以下方式使用列表推导而不使用任何
append
:
a=np.array([[1,2],[3,4]])
b = [i for i in a]
print (b)
Output 输出量
[array([1, 2]), array([3, 4])]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.