Replacing repeated consecutive characters in Python

后端 未结 5 656
抹茶落季
抹茶落季 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 06:00

    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.']
    

提交回复
热议问题