Re: Micro-PEP: str.translate(None) to mean identity translation



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
.



Relevant Pages

  • Bridge Double dummy - from C to Delphi #1
    ... should my INITIAL translation do a better job of type casting. ... I would certainly type cast many variables but should I ... char winning_suit, winning_rank, ... function abs_player(who: THand): THand; ...
    (comp.lang.pascal.delphi.misc)
  • Re: Micro-PEP: str.translate(None) to mean identity translation
    ... >> Bengt Richter ... characters, each and all of whose instances in the source string ... will be deleted from the source string. ... translation table, which must be a string of length 256. ...
    (comp.lang.python)
  • Re: tr-alike character replace function
    ... char of the input string, and for each one then look each char of the ... As the character range is really small (256 elements, ... What's wrong with just setting up a hard-coded 256 byte translation table ...
    (comp.lang.c)
  • Re: Micro-PEP: str.translate(None) to mean identity translation
    ... Bengt Richter wrote: ... > signifying no translation, but allowing easy expression of deleting ...
    (comp.lang.python)
  • C to Pascal
    ... char *rec; ... var rec: ^char? ... And what's the translation of the following function header: ...
    (comp.lang.pascal.borland)