By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,763 Members | 1,563 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,763 IT Pros & Developers. It's quick & easy.

looping question 4 NEWB

P: n/a
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

Jul 6 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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
Jul 6 '06 #2

P: n/a
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
Jul 6 '06 #3

P: n/a
But what about substitutions like:
'ab' 'cd', 'ced' 'de', etc

what is the fastest way then?
Roel Schroeven wrote:
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
Jul 6 '06 #4

P: n/a
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

Jul 6 '06 #5

P: n/a
manstey schreef:
Roel Schroeven wrote:
>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.

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

Roel Schroeven
Jul 6 '06 #6

P: n/a
In <11*********************@m73g2000cwd.googlegroups. com>, manstey wrote:
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
Jul 6 '06 #7

P: n/a
be************@lycos.com wrote:
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
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
Jul 6 '06 #8

P: n/a
Thanks Marc, that was very helpful.

Marc 'BlackJack' Rintsch wrote:
In <11*********************@m73g2000cwd.googlegroups. com>, manstey wrote:
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
Jul 9 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.