Re: Micro-PEP: str.translate(None) to mean identity translation
- From: Peter Otten <__peter__@xxxxxx>
- Date: Sat, 30 Apr 2005 09:23:45 +0200
M.E.Farmer wrote:
> Bengt Richter wrote:
>> Just thought None as the first argument would be both handy and
> mnemonic,
>> signifying no translation, but allowing easy expression of deleting
> characters,
>> e.g.,
>>
>> s = s.translate(None, 'badcharshere')
>>
>> Regards,
>> Bengt Richter
>
> What's wrong with :
>
> s = s.replace('badchars', "")
>
> It seems obvious to replace a char ( to me anyway ) with a blank
> string, rather than to 'translate' it to None.
> I am sure you have a reason what am I missing ?
> M.E.Farmer
>>> s = "NHoBwA RyAoSuB AsAeHeH AiBtH,A CnRoAwD HyBoAuC HdRoCnH'AtB"
>>> s.translate(None, "BADCHARS")
"Now you see it, now you don't"
>>> s.replace("BADCHARS", "")
"NHoBwA RyAoSuB AsAeHeH AiBtH,A CnRoAwD HyBoAuC HdRoCnH'AtB"
i. e. you have to replace() for every char in "BADCHARS":
>>> reduce(lambda x, y: x.replace(y, ""), "BADCHARS", s)
"Now you see it, now you don't"
+1, btw.
Peter
.
- References:
- Micro-PEP: str.translate(None) to mean identity translation
- From: Bengt Richter
- Re: Micro-PEP: str.translate(None) to mean identity translation
- From: M.E.Farmer
- Micro-PEP: str.translate(None) to mean identity translation
- Prev by Date: Re: [wxPython] Many wxPanel forms in 1 wxFrame
- Next by Date: Re: Micro-PEP: str.translate(None) to mean identity translation
- Previous by thread: Re: Micro-PEP: str.translate(None) to mean identity translation
- Next by thread: Re: Micro-PEP: str.translate(None) to mean identity translation
- Index(es):
Relevant Pages
|