list - python 将列表分割为大约相等长度的N 部分

  显示原文与译文双语对照的内容
128 0

什么是最好的方法把一个列表分成几个部分? 例如,如果列表有 7个元素并将它的拆分为 2个部分,则需要在一个部分中使用 3元素。

我正在寻找类似 even_split(L, n)的东西,把 L 分成 n 部分。


def chunks(L, n):


""" Yield successive n-sized chunks from L.


"""


 for i in xrange(0, len(L), n):


 yield L[i:i+n]



上面的代码提供了 3个块,而不是 3块。 我可以简单地转换( 重复这个,并采取每一列的第一个元素,调用第一部分,然后将第二部分放在第二部分。),但这破坏了对项目的排序。

时间: 原作者:

80 2

这里有一个可以工作的:


def chunkIt(seq, num):


 avg = len(seq)/float(num)


 out = []


 last = 0.0



 while last <len(seq):


 out.append(seq[int(last):int(last + avg)])


 last += avg



 return out



测试:


>>> chunkIt(range(10), 3)


[[0, 1, 2], [3, 4, 5], [6, 7, 8, 9]]


>>> chunkIt(range(11), 3)


[[0, 1, 2], [3, 4, 5, 6], [7, 8, 9, 10]]


>>> chunkIt(range(12), 3)


[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]



原作者:
110 4

只要你不想要像连续块那样愚蠢的东西:


>>> def chunkify(lst,n):


... return [lst[i::n] for i in xrange(n)]


... 


>>> chunkify(range(13), 3)


[[0, 3, 6, 9, 12], [1, 4, 7, 10], [2, 5, 8, 11]]



原作者:
79 2

你可以简单地将它的编写为列表生成器:


def split(a, n):


 k, m = divmod(len(a), n)


 return (a[i * k + min(i, m):(i + 1) * k + min(i + 1, m)] for i in xrange(n))



例如:


>>> list(split(range(11), 3))


[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10]]



58 5

这是对 numpy.array_split *:的 raison


>>> L


[0, 1, 2, 3, 4, 5, 6, 7]


>>> print(*np.array_split(L, 3))


[0 1 2] [3 4 5] [6 7]


>>> print(*np.array_split(range(10), 4))


[0 1 2] [3 4 5] [6 7] [8 9]



*credit 至 Piraeus 6

原作者:
51 0

更改代码以生成 n 块而不是 n 块:


def chunks(l, n):


""" Yield n successive chunks from l.


"""


 newn = int(len(l)/n)


 for i in xrange(0, n-1):


 yield l[i*newn:i*newn+newn]


 yield l[n*newn-newn:]



l = range(56)


three_chunks = chunks (l, 3)


print three_chunks.next()


print three_chunks.next()


print three_chunks.next()



这给出了:


[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, 55]



这将给最终组分配额外的元素,这不是完美的,但是在你的规范中很好:- 元素会更好,而这将是最佳的选择。

你可以使用以下代码获得更平衡的输出:


#!/usr/bin/python


def chunks(l, n):


""" Yield n successive chunks from l.


"""


 newn = int(1.0 * len(l)/n + 0.5)


 for i in xrange(0, n-1):


 yield l[i*newn:i*newn+newn]


 yield l[n*newn-newn:]



l = range(56)


three_chunks = chunks (l, 3)


print three_chunks.next()


print three_chunks.next()


print three_chunks.next()



输出:


[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, 55]



原作者:
92 2

这里是添加 None 以使列表长度相等的方法


>>> from itertools import izip_longest


>>> def chunks(l, n):


""" Yield n successive chunks from l. Pads extra spaces with None


"""


 return list(zip(*izip_longest(*[iter(l)]*n)))



>>> l=range(54)



>>> chunks(l,3)


[(0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51), (1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49, 52), (2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47, 50, 53)]



>>> chunks(l,4)


[(0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52), (1, 5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 53), (2, 6, 10, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50, None), (3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51, None)]



>>> chunks(l,5)


[(0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50), (1, 6, 11, 16, 21, 26, 31, 36, 41, 46, 51), (2, 7, 12, 17, 22, 27, 32, 37, 42, 47, 52), (3, 8, 13, 18, 23, 28, 33, 38, 43, 48, 53), (4, 9, 14, 19, 24, 29, 34, 39, 44, 49, None)]



原作者:
85 3

查看一下 numpy.split:


>>> a = numpy.array([1,2,3,4])


>>> numpy.split(a, 2)


[array([1, 2]), array([3, 4])]



...