簡體   English   中英

在numpy 2-dim數組中堆疊列以獲得1-dim數組

[英]Stack columns in numpy 2-dim array to get 1-dim array

如何將二維Numpy數組中的所有列堆疊成一維數組。

即我有:

x = np.array([[1, 3, 5],[2, 4, 6]])

我想得到:

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

有沒有辦法在沒有循環或列表理解的情況下實現這一點?

你可以使用ravel

x = np.array([[1, 3, 5],[2, 4, 6]])

res = x.ravel('F')  # or x.T.ravel()

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

使用'F' flatten

x.flatten('F')
Out[114]: array([1, 2, 3, 4, 5, 6])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM