You can create an iterator - without trying to be smart, just to make it short and unreadable:
def yield_same(string): it_str = iter(string) result = it_str.next() for next_chr in it_str: if next_chr != result[0]: yield result result = "" result += next_chr yield result .. >>> list(yield_same("aaaaaabcbcdcdccccccdddddd")) ['aaaaaa', 'b', 'c', 'b', 'c', 'd', 'c', 'd', 'cccccc', 'dddddd'] >>>
change ok, so there is itertools.groupby, which probably does something like this.
jsbueno
source share