簡體   English   中英

Python Numpy 將數組重塑為具有少於 m*n 個元素的 (m,n) 形狀

[英]Python Numpy Reshape an array to (m,n) shape that has less than m*n elements

我正在嘗試將一個簡單的數組轉換為 (m,n) 形狀,但它的元素少於 m*n。

我的代碼:

list = [1,2,3,4,5]
ary = np.array(list)
reary = ary.reshpae(2,3)

現回答:

ValueError: cannot reshape array of size 5 into shape (2,3)

預期答案:

reary = 

[[1,2,3],
 [4,5]]

嘗試這個:

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

r, c = 2, 3
a = np.pad(ary, (0, r * c - len(ary))).reshape(r, c)
>>> a
array([[1, 2, 3],
       [4, 5, 0]])

暫無
暫無

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

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