python numpy将数组分成不等的子数组

use*_*022 7 python split numpy list

我试图将一个数组分成n个部分.有时这些部件的尺寸相同,有时它们的尺寸不同.

我正在尝试使用:

split = np.split(list, size)
Run Code Online (Sandbox Code Playgroud)

当大小平均分配到列表中时,这可以正常工作,但否则失败.有没有办法做到这一点,用最多'少数'元素"填充"最终数组?

PhM*_*hML 27

你在找np.array_split吗?这是docstring:

Split an array into multiple sub-arrays.

Please refer to the ``split`` documentation.  The only difference
between these functions is that ``array_split`` allows
`indices_or_sections` to be an integer that does *not* equally 
divide the axis.

See Also
--------
split : Split array into multiple sub-arrays of equal size.

Examples
--------
>>> x = np.arange(8.0)
>>> np.array_split(x, 3)
    [array([ 0.,  1.,  2.]), array([ 3.,  4.,  5.]), array([ 6.,  7.])]
Run Code Online (Sandbox Code Playgroud)

http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.array_split.html


YXD*_*YXD 3

def split_padded(a,n):
    padding = (-len(a))%n
    return np.split(np.concatenate((a,np.zeros(padding))),n)
Run Code Online (Sandbox Code Playgroud)