splitting a list into n groups
Peter Otten
__peter__ at web.de
Wed Oct 8 16:49:49 EDT 2003
Paul Rubin wrote:
> Rajarshi Guha <rajarshi at presidency.com> writes:
>> is there an efficient (pythonic) way in which I could split a list into
>> say 5 groups? By split I mean the the first x members would be one group,
>> the next x members another group and so on 5 times. (Obviously x =
>> lengthof list/5)
>
> groups = [a[i:i+(len(a)//5)] for i in range(5)]
>>> for k in range(10):
... a = range(k)
... print [a[i:i+(len(a)//5)] for i in range(5)]
...
[[], [], [], [], []]
[[], [], [], [], []]
[[], [], [], [], []]
[[], [], [], [], []]
[[], [], [], [], []]
[[0], [1], [2], [3], [4]]
[[0], [1], [2], [3], [4]]
[[0], [1], [2], [3], [4]]
[[0], [1], [2], [3], [4]]
[[0], [1], [2], [3], [4]]
>>>
Is that what you expected?
Peter
More information about the Python-list
mailing list