Python split list into n chunks

前端 未结 17 1440
情深已故
情深已故 2020-12-02 13:42

I know this question has been covered many times but my requirement is different.

I have a list like: range(1, 26). I want to divide this list into a fi

17条回答
  •  情深已故
    2020-12-02 13:49

    Use numpy

    >>> import numpy
    >>> x = range(25)
    >>> l = numpy.array_split(numpy.array(x),6)
    

    or

    >>> import numpy
    >>> x = numpy.arange(25)
    >>> l = numpy.array_split(x,6);
    

    You can also use numpy.split but that one throws in error if the length is not exactly divisible.

提交回复
热议问题