String Splitter Brain Teaser

Bill Mill bill.mill at gmail.com
Mon Mar 28 09:33:35 EST 2005


On 28 Mar 2005 04:12:15 -0800, bearophileHUGS at lycos.com
<bearophileHUGS at lycos.com> 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] == '/':
            e.next()
            i2, c2 = e.next()
            yield [c, c2]
        else:
            yield [c]

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

Peace
Bill Mill
bill.mill at gmail.com



More information about the Python-list mailing list