Danail Lesev
Danail Lesev

Reputation: 45

Replace sequence of the same letter with single one

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:

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

Upvotes: 1

Views: 421

Answers (2)

Jab
Jab

Reputation: 27485

Using re:

import re

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

Using zip:

"".join(x for x, y in zip(text, text[1:]) if x!= y) + text[-1]

Using itertools.groupby:

from itertools import groupby
from operator import itemgetter

"".join(map(itemgetter(0), groupby(text)))

Upvotes: 4

pho
pho

Reputation: 25489

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))

Upvotes: 1

Related Questions