Replacing repeated consecutive characters in Python

后端 未结 5 654
抹茶落季
抹茶落季 2021-01-23 05:02

I need to make a function that replaces repeated, consecutive characters with a single character, for example:

 \'hiiii how are you??\' -> \'hi how are you?\'         


        
5条回答
  •  北荒
    北荒 (楼主)
    2021-01-23 05:56

    You can try a regular expression like (.)\1+, i.e. "something, then more of the same something", and replace it with \1, i.e. "that first something".

    >>> import re
    >>> re.sub(r"(.)\1+", r"\1", 'aahhhhhhhhhh whyyyyyy')
    'ah why'
    >>> re.sub(r"(.)\1+", r"\1", 'oook. thesse aree enoughh examplles.')
    'ok. these are enough examples.'
    

    Make it a function with functools.partial (or any other way you like)

    >>> import functools
    >>> dedup = functools.partial(re.sub, r"(.)\1+", r"\1")
    >>> dedup('oook. thesse aree enoughh examplles.')
    'ok. these are enough examples.'
    

提交回复
热议问题