自动填零数组

Bas*_*asj 3 python arrays numpy list

让我们L列出55项:

L=range(55)
for i in range(6):
 print L[10*i:10*(i+1)]
Run Code Online (Sandbox Code Playgroud)

打印列表将有10项i = 0,1,2,3,4,但对于i = 5,它将只有5项.

是否有一种快速的自动零填充方法L [50:60],因此它是10项长?

Ash*_*ary 7

使用NumPy:

>>> a = np.arange(55)
>>> a.resize(60)
>>> a.reshape(6, 10)
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
       [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
       [50, 51, 52, 53, 54,  0,  0,  0,  0,  0]])
Run Code Online (Sandbox Code Playgroud)