首页 > 解决方案 > How to fill 2d numpy array with some 2d numpy array

问题描述

I have a 2d numpy array which varies in size from one value to other.

How can I generalize the size by filling in zeros if the input value has less dimension than required?

Exmaple:

Input list with numpy array dimension:
[(40, 173),
(40, 14),
(40, 56),
(40, 173)]

And I want to have all the arrays to be (40, 173), where if it is has lesser size, than filling rest with zeros.

标签: pythonnumpy

解决方案


分配给zeros数组:

In [160]: res = np.zeros((40,173),int)                                                               
In [161]: res[0:20, 0:30] = np.ones((20,30),int)  

推荐阅读