The Python itertools module has powerful tools for grouping and then for iterating through group members leading to the next program.
I showed some intermediate results and used the pprint module to print the answer:
Python 2.7.3 (default, Aug 1 2012, 05:16:07) [GCC 4.6.3] on linux2 Type "copyright", "credits" or "license()" for more information. >>> import itertools >>> instring = "cccaatt" >>> [(x[0], list(x[1])) for x in itertools.groupby(instring)] [('c', ['c', 'c', 'c']), ('a', ['a', 'a']), ('t', ['t', 't'])] >>> xx = [list(x[0]*n for n in range(1, len(list(x[1]))+1)) for x in itertools.groupby(instring)] >>> xx [['c', 'cc', 'ccc'], ['a', 'aa'], ['t', 'tt']] >>> list(itertools.product(*xx)) [('c', 'a', 't'), ('c', 'a', 'tt'), ('c', 'aa', 't'), ('c', 'aa', 'tt'), ('cc', 'a', 't'), ('cc', 'a', 'tt'), ('cc', 'aa', 't'), ('cc', 'aa', 'tt'), ('ccc', 'a', 't'), ('ccc', 'a', 'tt'), ('ccc', 'aa', 't'), ('ccc', 'aa', 'tt')] >>> from pprint import pprint as pp >>> pp(list(itertools.product(*xx))) [('c', 'a', 't'), ('c', 'a', 'tt'), ('c', 'aa', 't'), ('c', 'aa', 'tt'), ('cc', 'a', 't'), ('cc', 'a', 'tt'), ('cc', 'aa', 't'), ('cc', 'aa', 'tt'), ('ccc', 'a', 't'), ('ccc', 'a', 'tt'), ('ccc', 'aa', 't'), ('ccc', 'aa', 'tt')] >>>
Or as a function:
>>> def stringexpand(instring): xx = [list(x[0]*n for n in range(1, len(list(x[1]))+1)) for x in itertools.groupby(instring)] return list(itertools.product(*xx)) >>> pp(stringexpand("cccaatt")) [('c', 'a', 't'), ('c', 'a', 'tt'), ('c', 'aa', 't'), ('c', 'aa', 'tt'), ('cc', 'a', 't'), ('cc', 'a', 'tt'), ('cc', 'aa', 't'), ('cc', 'aa', 'tt'), ('ccc', 'a', 't'), ('ccc', 'a', 'tt'), ('ccc', 'aa', 't'), ('ccc', 'aa', 'tt')] >>>
It seems you need strings connected to their parts. This can be done in this small mod:
def stringexpand(instring): xx = [list(x[0]*n for n in range(1, len(list(x[1]))+1)) for x in itertools.groupby(instring)] return [''.join(parts) for parts in itertools.product(*xx)]
What returns:
['cat', 'catt', 'caat', 'caatt', 'ccat', 'ccatt', 'ccaat', 'ccaatt', 'cccat', 'cccatt', 'cccaat', 'cccaatt']