String Splitter Brain Teaser

Bill Mill bill.mill at
Mon Mar 28 16:33:35 CEST 2005

On 28 Mar 2005 04:12:15 -0800, bearophileHUGS at
<bearophileHUGS at> wrote:
> This is shorter:
> map(list,' '.join(s).replace(' / ','').split())
> but for very long genomes Michael Spencer's nice version can be faster.

for very long genomes he might want a generator:

def xgen(s):
    l = len(s) - 1
    e = enumerate(s)
    for i,c in e:
        if i < l and s[i+1] == '/':
            i2, c2 =
            yield [c, c2]
            yield [c]

>>> for g in xgen('ATT/GATA/G'): print g
['T', 'G']
['A', 'G']

Bill Mill
bill.mill at

More information about the Python-list mailing list