alternating string replace

Pablo Ziliani pablo at decode.com.ar
Fri Jan 11 21:55:47 EST 2008


Paul Rubin wrote:
> George Sakkis <george.sakkis at gmail.com> writes:
>   
>> from itertools import chain, izip, cycle
>> print ''.join(chain(*izip(s1.split('_'),cycle(':,'))))[:-1]
>>     
>
> from itertools import cycle
> a = cycle(':,')
> print re.sub('_', lambda x: a.next(), s1)
>   

Lovely.

If there OP didn't vanished into thin air*, I'd declare you the winner 
(although you forgot to import re).
This said, I'm still kind of partial to any re-only solution, but it 
would require someone outlines the behavior in the corner cases.

* die, thread!

--
Pablo



More information about the Python-list mailing list