Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove repeated letters in Python

Tags:

python

string

How do i remove repeated letters in a string?

Tried this without success..

def shorten_string(char_str):
    new=''
    for i in range(0,len(char_str)-1):
       if char_str[i-1] != char_str[i]:
           new += char_str[i]
return new

EDIT: Misunderstanding: i do not want to delete all repeated characthers. Just if they are repeated in order.

input: lloolleellaa
outpu: lolela
like image 928
Sander B Avatar asked Dec 02 '22 13:12

Sander B


2 Answers

Removing adjacent equal items can be done as follows with groupby:

>>> import itertools
>>> ''.join(c[0] for c in itertools.groupby('haalllooo'))
'halo'

This simply takes the heads of each of the groups of equal items.

>>> ''.join(c[0] for c in itertools.groupby('haalllooo thheeerrree tttthhhiiisss iiisss aaann eeeexxxaaammpppllleee'))
'halo there this is an example'

To keep only the unique items in order:

def unique(it):
    s = set()
    for x in it:
        if x not in s:
           s.add(x)
           yield x

This can be used like this:

>>> ''.join(unique('haalllooo'))
'halo'
>>> ''.join(unique('haalllooo thheeerrree tttthhhiiisss iiisss aaann eeeexxxaaammpppllleee'))
'halo terisnxmp'
like image 110
Dan D. Avatar answered Dec 20 '22 07:12

Dan D.


It is the same logic as is for all languages. This is a frequent question asked in interviews. Basically you assign each character of the string to a data structure. The choice of the data structure differs from language and performance. Sometimes they might also ask if the order matters or not.

>>> foo = 'haalllooo'
>>> ''.join(sorted(set(foo), key=foo.index))
'halo'
like image 27
roymustang86 Avatar answered Dec 20 '22 06:12

roymustang86