Re: string replace
- From: Tim Chase <python.list@xxxxxxxxxxxxxxxxx>
- Date: Fri, 30 Jun 2006 09:23:18 -0500
a lot of times I need to replace more than one char into a
string, so I have to do something like
value = "test"
chars = "e"
for c in chars:
value = value.replace(c, "")
A solution could be that "replace" accept a tuple/list of
chars, like that was add into the new 2.5 for startswith.
I don't know, but can be this feature included into a future
python release?
Well, another way of doing it would be
>>> values = "this is a test"
>>> chars = "aeiou"
>>> "".join([c for c in values if c not in chars])
'ths s tst'
If your either your chars is a large set or you're performing this repeatedly with the same set of chars, you might want the speed of membership-testing that one would get from a true set:
>>> charset = set(chars) # do this once for the set
>>> # do the following as many times as you like in loops, etc.
>>> "".join([c for c in values if c not in charset])
'ths s tst'
HTH,
-tkc
.
- References:
- string replace
- From: Michele Petrazzo
- string replace
- Prev by Date: Re: Icono en wxpython
- Next by Date: Chapter 9 Tutorial for Classes Not Working
- Previous by thread: Re: string replace
- Next by thread: Re: string replace
- Index(es):
Relevant Pages
|