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?\'
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.'