<div class="gmail_quote">On Tue, Dec 15, 2009 at 12:19 PM, Luhmann <span dir="ltr"><<a href="mailto:luhmann_br@yahoo.com">luhmann_br@yahoo.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td style="font-family: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; font-size: inherit; line-height: inherit; font-size-adjust: inherit; font-stretch: inherit;" valign="top">
Hi folks,<br><br>I'm trying to do something like this:<br><br>>>> evildict= {'good' : 'bad' , 'love' : 'hate' , 'God': 'Satan'}<br><br>>>> def make_evil(text)<br>
... for a in evildict:<br>... text=text.replace(a, evildict[a])<br>... return text<br> <br><br>This works fine, but it soon gets too slow as the size of text and dictionary begin to grow.<br>
Can you guys suggest me a way to make it faster?<br><br></td></tr></tbody></table></blockquote><div>Build a regex out of the keys. Provide a function to re.sub() that looks up the match in the dict and returns the associated value. See<br>
<a href="http://code.activestate.com/recipes/81330/">http://code.activestate.com/recipes/81330/</a><br><br>Kent <br></div></div><br>