looping question 4 NEWB

Wolfram Kraus kraus at hagen-partner.de
Thu Jul 6 07:08:45 EDT 2006


On 06.07.2006 12:43, manstey wrote:
> Hi,
> 
> I often have code like this:
> 
> data='asdfbasdf'
> find = (('a','f')('s','g'),('x','y'))
> for i in find:
>    if i[0] in data:
>        data = data.replace(i[0],i[1])
> 
> is there a faster way of implementing this? Also, does the if clause
> increase the speed?
> 
> Thanks,
> Matthew
> 

>>> import string
>>> data='asdfbasdf'
>>> data.translate(string.maketrans('asx', 'fgy'))
'fgdfbfgdf'

HTH,
Wolfram



More information about the Python-list mailing list