Replace Several Items

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Wed Aug 13 21:54:55 EDT 2008


On Wed, 13 Aug 2008 22:50:29 +0000, Wojtek Walczak wrote:

> Dnia Thu, 14 Aug 2008 00:31:00 +0200, Fredrik Lundh napisa³(a):
> 
>>>>      if ch in my_string:
>>>>           my_string = my_string.replace(ch, "")
>>>>
>>>> on representative data.
>>> 
>>> I don't have to, I can anticipate the results.
>>
>> Chances are that you're wrong.
> 
> At the moment my average is about 0.75 of mistake per post on
> comp.lang.python (please, bare with me ;-)). I strongly believe that the
> statement I made above won't make this number rise.
> 
> :)



Okay, is this going to be one of those things where, no matter what the 
benchmarks show, you say "I was right, I *did* anticipate the results. I 
just anticipated them correctly/incorrectly."?

If so, you get an A+ in pedantry and F- in usefulness *wink*

In full knowledge that Python is relatively hard to guess what is fast 
compared to what is slow, I'll make my guess of fastest to slowest:

1. repeated replace
2. repeated use of the form 
   "if ch in my_string: my_string = my_string.replace(ch, "")
3. re.sub with literal replacement
4. re.sub with callback (lambda m: "")


Results to follow.


-- 
Steven



More information about the Python-list mailing list