List Splitting

Neil Cerutti cerutti at tds.net
Mon Aug 21 15:42:17 EDT 2006


On 2006-08-21, Steven <sfaulconer at gmail.com> wrote:
> Hello everyone,
>
> I'm trying to work through a bit of a logic issue I'm having with a
> script I'm writing. Essentially, I have a list that's returned to
> me from another command that I need to regroup based on some aribitrary
> length.
>
> For the purposes of this question, the list will be:
>
> t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ]
>
> Now, I know that every 3rd element of the list belongs together:
>
> Group 1 = 0, 3, 6
> Group 2 = 1, 4, 7
> Group 3 = 2, 5, 8

from itertools import islice

grouped = []
grouped.append(list(islice(t, 0, None, 3))
grouped.append(list(islice(t, 1, None, 3))
grouped.append(list(islice(t, 2, None, 3))
grouped.sort()

This can probably be simplified and generalized, but I'm a novice, and 
that's a start.

--
Neil Cerutti



More information about the Python-list mailing list