Вариант #1 (pythonic):
from itertools import groupby
def squeeze(text):
return ''.join(key for key, group in groupby(text))
squeeze('aAaaabbccdcc') # => 'aAabcdc'
Вариант #2 (примитивный):
def squeeze(text):
for c1, c2 in zip(text[:-1], text[1:]):
if c1 != c2:
yield c1
yield c2
''.join(squeeze('aAaaabbccdcc')) # => 'aAabcdc'
Вариант #3 (говнокод, но для разнообразия):
from operator import ne
from itertools import compress, chain
def squeeze(text):
return ''.join(chain(compress(text, map(ne, text[:-1], text[1:])), text[-1]))
squeeze('aAaaabbccdcc') # => 'aAabcdc'