substitution

Anthra Norell anthra.norell at bluewin.ch
Mon Jan 18 11:26:45 EST 2010


superpollo wrote:
>> hi.
>>
>> what is the most pythonic way to substitute substrings?
>>
>> eg: i want to apply:
>>
>> foo --> bar
>> baz --> quux
>> quuux --> foo
>>
>> so that:
>>
>> fooxxxbazyyyquuux --> barxxxquuxyyyfoo
>>
>> bye
>
>

Third attempt. Clearly something doesn't work right. My code gets 
clipped on the way up. I have to send it as an attachment. Here's again 
what it does:

 >>> substitutions = (('foo', 'bar'), ('baz', 'quux'), ('quuux', 
'foo'))   # Sequence of doublets
 >>> T = Translator (substitutions)   # Compile substitutions -> translator
 >>> s = 'fooxxxbazyyyquuux'   # Your source string
 >>> d = 'barxxxquuxyyyfoo'    # Your destination string
 >>> print T (s)
barxxxquuxyyyfoo
 >>> print T (s) == d
True


Code attached

Regards

Frederic


-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: translator.py
URL: <http://mail.python.org/pipermail/python-list/attachments/20100118/c6b51ee8/attachment.ksh>


More information about the Python-list mailing list