简体   繁体   English

Python Numpy 将数组重塑为具有少于 m*n 个元素的 (m,n) 形状

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

I am trying to convert a simple array into (m,n) shape but it has less than m*n elements.我正在尝试将一个简单的数组转换为 (m,n) 形状,但它的元素少于 m*n。

My code:我的代码:

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

Present answer:现回答:

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

Expected answer:预期答案:

reary = 

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

Try this:尝试这个:

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