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?\'
A solution can be expressed very compactly using itertools.groupby:
>>> import itertools
>>> ''.join(g[0] for g in itertools.groupby('hiiii how are you??'))
'hi how are you?'
itertools.groupby
groups the objects in an iterable by the given key function. Groups are accumulated as long as the keys are equivalent. If no key function is given, the identity of the items are used, in this case the characters.
Once you have them grouped by their identity, you can then join the objects into a single string. The grouped objects are returned as tuples containing the object and an internal itertools._grouper
object, which for your purposes, you can ignore and extract the character.
This can be turned into a function as follows:
def remove_repeated_characters(s):
groups = itertools.groupby(s)
cleaned = ''.join(g[0] for g in groups)
return cleaned
This results in the expected values:
>>> [remove_repeated_characters(s)
for s in ['hiiii how are you??','aahhhhhhhhhh whyyyyyy',
'foo', 'oook. thesse aree enoughh examplles.']]
['hi how are you?', 'ah why', 'fo', 'ok. these are enough examples.']