String Splitter Brain Teaser

Mike Rovner mrovner at
Mon Mar 28 06:21:27 CEST 2005

Jp Calderone wrote:
> On Sun, 27 Mar 2005 14:39:06 -0800, James Stroud <jstroud at> wrote:
>>gets split to
>>[['A'], ['T'], ['T', 'G'], ['A'], ['T'], ['A', 'G']]
>>I have written a very ugly function to do this (listed below for the curious), 
>>but intuitively I think this should only take a couple of lines for one 
>>skilled in regex and/or listcomp. Any takers?
>     >>> import re
>     >>> s = 'ATT/GATA/G'
>     >>> re.findall('(./.|.)', s)
>     ['A', 'T', 'T/G', 'A', 'T', 'A/G']
>     >>> 
>   If it is really important to have ['A'] instead of 'A', etc, looping over the result and noticing strings of length 3 vs length 1, then applying the appropriate transformation, should be simple enough.

 >>> [x.split('/') for x in ['A', 'T', 'T/G', 'A', 'T', 'A/G']]
[['A'], ['T'], ['T', 'G'], ['A'], ['T'], ['A', 'G']]


More information about the Python-list mailing list