Numpy连接,使用*或类似的符号

问题描述 投票:0回答:1

我有一个numpy数组的列表。像这样的东西(不是相同的例子,而是相似的)]

lst = [np.array([ 1,2,3,4,5,6 ]).reshape(-1, 1), np.array([ 1,2,3,4,5,6 ]).reshape(-1, 1), np.array([ 1,2,3,4,5,6 ]).reshape(-1, 1)]

在这种情况下,我的lst具有3个numpy数组,它们的形状为(6,1),现在我想将其连接起来,如下所示:

# array([[1, 1, 1],
#        [2, 2, 2],
#        [3, 3, 3],
#        [4, 4, 4],
#        [5, 5, 5],
#        [6, 6, 6]])

并且这样做很完美...

example = np.c_[lst[0], lst[1], lst[2]]

但是我的lst并不总是相同的大小,所以我尝试了这个。

example = np.c_[*lst]

但是它不起作用。有没有办法以这种方式将整个列表串联起来?

python numpy
1个回答
0
投票

您可以使用column_stack功能:

import numpy as np

lst = [np.array([1, 2, 3, 4, 5, 6]).reshape(-1, 1), np.array([1, 2, 3, 4, 5, 6]).reshape(-1, 1), np.array([1, 2, 3, 4, 5, 6]).reshape(-1, 1)]

example = np.column_stack(lst)
print(example)
[[1 1 1]
 [2 2 2]
 [3 3 3]
 [4 4 4]
 [5 5 5]
 [6 6 6]]
© www.soinside.com 2019 - 2024. All rights reserved.