我有一個 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]但它不起作用。有沒有辦法以這種方式連接整個列表?
1 回答

慕森王
TA貢獻1777條經驗 獲得超3個贊
您可以使用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]]
添加回答
舉報
0/150
提交
取消