Home > Net >  Replace sequence of the same letter with single one
Replace sequence of the same letter with single one

Time:07-15

I am trying to replace the number of letters with a single one, but seems to be either hard either I am totally block how this should be done So example of input:

  • aaaabbcddefff

The output should be abcdef Here is what I was able to do, but when I went to the last piece of the string I can't get it done. Tried different variants, but I am stucked. Can someone help me finish this code?

text = "aaaabbcddefff"

new_string = ""

count = 0

while text:

for i in range(len(text)):

    l = text[i]

    for n in range(len(text)):

        if text[n] == l:
            count  = 1
            continue

        new_string  = l
        text = text.replace(l, "", count)
        break

    count = 0
    break

CodePudding user response:

Using regex

re.sub(r"(.)(?=\1 )", "", text)

>>> import re
>>> text = "aaaabbcddefff"
>>> re.sub(r"(.)(?=\1 )", "", text)
abcdeaf

CodePudding user response:

Side note: You should consider building your string up in a list and then joining the list, because it is expensive to append to a string, since strings are immutable.

One way to do this is to check if every letter you look at is equal to the previous letter, and only append it to the new string if it is not equal:

def remove_repeated_letters(s):
    if not s: return ""

    ret = [s[0]] 
    for index, char in enumerate(s[1:], 1):
        if s[index-1] != char:
            ret.append(char)

    return "".join(ret)

Then, remove_repeated_letters("aaaabbcddefff") gives 'abcdef'.
remove_repeated_letters("aaaabbcddefffaaa") gives 'abcdefa'.

Alternatively, use itertools.groupby, which groups consecutive equal elements together, and join the keys of that operation

import itertools

def remove_repeated_letters(s):
    return "".join(key for key, group in itertools.groupby(s))
  • Related