Pythonic Way of replacing Multiple Characters

后端 未结 2 508
我寻月下人不归
我寻月下人不归 2021-01-15 22:22

I\'ve created a onetime function

a = lambda x: x.replace(\'\\n\', \'\')
b = lambda y: y.replace(\'\\t\', \'\').strip()
c = lambda x: b(a(x))
<
相关标签:
2条回答
  • 2021-01-15 22:50

    Option 1
    str.translate
    For starters, if you're replacing a lot of characters with the same thing, I'd 100% recommend str.translate.

    >>> from string import whitespace as wsp
    >>> '\n\ttext   \there\r'.translate(str.maketrans(dict.fromkeys(wsp, '')))
    'texthere'
    

    This syntax is valid with python-3.x only. For python-2.x, you will need to import string and use string.maketrans to build the mapping instead.

    If you want to exclude whitespace chars itself, then

    wsp = set(wsp) - {' '}
    

    Option 2
    re.sub
    The regex equivalent of the above would be using re.sub.

    >>> import re
    >>> re.sub(r'\s+', '', '\n\ttext   \there\r')
    'texthere'
    

    However, performance wise, str.translate beats this hands down.

    0 讨论(0)
  • 2021-01-15 22:51

    The improvements are pretty straightforward:

    Drop lambdas. str.replace() method is a function, and in the first line of your snippet you define a function that calls to another function and nothing else. Why do you need the wrapping lambda? The same concerns the second line.

    Use return values. Actually, in docs we see:

    Return a copy of the string with all occurrences of substring old replaced by new.

    So you can do a first replace(), then do a second one on the obtained result.

    To sum up, you'll have:

    c = x.replace('\n', '').replace('\t', '').strip()
    

    Note: if you have many characters to remove, you'd better use str.translate() but for two of them str.replace() is far more readable.

    Cheers!

    0 讨论(0)
提交回复
热议问题