Deleting specific characters from a string
Paul Rudin
paul_rudin at scientia.com
Thu Jul 10 07:06:08 EDT 2003
>>>>> "Jeff" == Jeff Hinrichs <jlh at cox.net> writes:
> I've been following this thread, and on a whim I built a test
> harness to time the different ideas that have been put forth in
> this thread. I will post complete results tomorrow on the web
> but the short version is that using the .replace method is the
> overall champ by quite a bit. Below is the function I tested
> against the others in the harness:
> def stringReplace(s,c): """Remove any occurrences of characters
> in c, from string s s - string to be filtered, c - characters to
> filter""" for a in c: s = s.replace(a,'') return s
> It wins also by being easy to understand, no filter or lambda.
> Not that I have anything against filter or lambda, but when the
> speediest method is the most readable, that solution is
> definitely the Pythonic champ. :)
I haven't been following this thread closely but isn't a regexp the
obvious way to do this? I'd expect it to be faster than your solution
- particularly on large input (although I haven't actually
tried). Arguably it's more pythonic too :-)
re.compile(r).sub('',s)
where r is the obvious disjunctive regexp mentioning each of the
charaters you want to remove. If you want to construct such a regexp
from a list of characters:
r= reduce(lambda x,y: x+'|'+y, c,'')[1:]
So putting it all together as an alternative version of your fuction:
!!warning - untested code!!
import re
def stringReplace(s,c):
r= reduce(lambda x,y: x+'|'+y, c,'')[1:]
return re.compile(r).sub('',s)
More information about the Python-list
mailing list