looping question 4 NEWB

M

manstey

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
 
R

Roel Schroeven

manstey schreef:
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?

I think this is best done with translate() and string.maketrans() (see
http://docs.python.org/lib/node110.html#l2h-835 and
http://docs.python.org/lib/string-methods.html#l2h-208). An example:

import string

data = 'asdfbasdf'
translatetable = string.maketrans('asx', 'fgy')
data = data.translate(translatetable)
print data

This results in:

fgdfbfgdf
 
W

Wolfram Kraus

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
'fgdfbfgdf'

HTH,
Wolfram
 
M

manstey

But what about substitutions like:
'ab' > 'cd', 'ced' > 'de', etc

what is the fastest way then?


Roel said:
manstey schreef:
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?

I think this is best done with translate() and string.maketrans() (see
http://docs.python.org/lib/node110.html#l2h-835 and
http://docs.python.org/lib/string-methods.html#l2h-208). An example:

import string

data = 'asdfbasdf'
translatetable = string.maketrans('asx', 'fgy')
data = data.translate(translatetable)
print data

This results in:

fgdfbfgdf

--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton

Roel Schroeven
 
B

bearophileHUGS

manstey:
is there a faster way of implementing this? Also, does the if clause
increase the speed?

I doubt the if increases the speed. The following is a bit improved
version:

# Original data:
data = 'asdfbasdf'
find = (('a', 'f'), ('s', 'g'), ('x', 'y'))

# The code:
data2 = data
for pat,rep in find:
data2 = data.replace(pat, rep)
print data2

# If find contains only chars, and the string is long
# enough, then this is more or less the faster solution:

from string import maketrans
table = map(chr, xrange(256))
for c1,c2 in find:
table[ord(c1)] = c2
table_str = "".join(table)
print data.translate(table_str)

Bye,
bearophile
 
R

Roel Schroeven

manstey schreef:
Roel said:
manstey schreef:
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?
I think this is best done with translate() and string.maketrans() (see
http://docs.python.org/lib/node110.html#l2h-835 and
http://docs.python.org/lib/string-methods.html#l2h-208). An example:
> But what about substitutions like:
> 'ab' > 'cd', 'ced' > 'de', etc
>
> what is the fastest way then?

Ah, in that case I don't think you can do much better than you already
did. But I think the if clause doesn't increase the speed; it might even
decrease it. If you want to know for sure, use timeit to see what's fastest.
 
S

Simon Forman

manstey:

I doubt the if increases the speed. The following is a bit improved
version:

# Original data:
data = 'asdfbasdf'
find = (('a', 'f'), ('s', 'g'), ('x', 'y'))

# The code:
data2 = data
for pat,rep in find:
data2 = data.replace(pat, rep)
print data2

Small bug in that code, you'll wind up with data2 only being the result
of replacing the last (pat, rep) in find. It should be:

data2 = data
for pat, rep in find:
data2 = data2.replace(pat, rep)

Be careful with multi-char terms in find. You could wind up replacing
patterns that only occur in data2 as a result of earlier replacements.

I.e. if
find = ('bc', 'ab'), ('aa', 'bb')
data = 'abc'

then
data2 = 'aab' # First iteration,
data2 = 'bbb' # Second iteration replaces 'aa' even though 'aa' isn't
in original data.

Have fun,
~Simon

# If find contains only chars, and the string is long
# enough, then this is more or less the faster solution:

from string import maketrans
table = map(chr, xrange(256))
for c1,c2 in find:
table[ord(c1)] = c2
table_str = "".join(table)
print data.translate(table_str)

Bye,
bearophile
 
M

Marc 'BlackJack' Rintsch

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?

It decreases it. You search through `data` in the ``if`` clause. If it's
`False` then you have searched the whole data and skip the replace. If
it's `True` you searched into data until there's a match and the the
`replace()` starts again from the start and searches/replaces through the
whole data.

You can get rid of the indexes and make the code a bit clearer by the way:

for old, new in find:
data = data.replace(old, new)

Ciao,
Marc 'BlackJack' Rintsch
 
M

manstey

Thanks Marc, that was very helpful.
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?

It decreases it. You search through `data` in the ``if`` clause. If it's
`False` then you have searched the whole data and skip the replace. If
it's `True` you searched into data until there's a match and the the
`replace()` starts again from the start and searches/replaces through the
whole data.

You can get rid of the indexes and make the code a bit clearer by the way:

for old, new in find:
data = data.replace(old, new)

Ciao,
Marc 'BlackJack' Rintsch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top