when does re take over from string.replace ?

sweeting at neuronet.com.my sweeting at neuronet.com.my
Thu May 27 07:50:49 EDT 1999


The python doc's say to use the string module instead of the re
module for simple tasks (and I'll readily admit that I'm glad to
hear that). Now I have a translation that goes somewhat like this :

oldwordlist = ['value1', 'value2', 'value3' ..... ]
replacelist = ['newvalue1', 'newvalue2', 'newvalue3' ...... ]
text = "about 2-3,000 characters of text........."

for i in range (0, len(oldwordlist)) :
    text = string.replace(text, oldwordlist[i], replacelist[i])


The list of words to be replaced is usually 10-30 long.
The bulk text on which this translation occurs is about 2,000
characters.  It works fine and I've no problems but since this
is being used often in my app, I figured I should try to
optimise it.

I have no idea how fast the built-in functions like
string.replace are but at what stage should I be looking at
doing this with a more complicated method that uses one pass
instead of the current method that requires 10-30 passes
depending on the replacelist. Would re be better suited even now ?

thanking you in advance,

chas


--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---




More information about the Python-list mailing list