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
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'
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With