List splitting

Klaus Alexander Seistrup klaus at seistrup.dk
Mon Aug 21 15:22:36 EDT 2006


Steven skrev:

> 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
>
> I'm trying to sort this list out so that I get a list of lists 
> that contain the correct elements:
>
> Goal = [ [ "a", "n", "t"], [ "b", "a", "t"],
> ["c", "a", "t" ] ]

#v+

>>> t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ]
>>> [t[i::3] for i in range(3)]
[['a', 'n', 't'], ['b', 'a', 't'], ['c', 'a', 't']]
>>> 

#v-

Cheers,

-- 
Klaus Alexander Seistrup
SubZeroNet, Copenhagen, Denmark
http://magnetic-ink.dk/



More information about the Python-list mailing list