Splitting a sequence into pieces with identical elements

candide candide at free.invalid
Tue Aug 10 20:37:08 EDT 2010


Suppose you have a sequence s , a string  for say, for instance this one :

spppammmmegggssss

We want to split s into the following parts :

['s', 'ppp', 'a', 'mmmm', 'e', 'ggg', 'ssss']

ie each part is a single repeated character word.

What is the pythonic way to answer this question?

A naive solution would be the following :


# -------------------------------
z='spppammmmegggssss'

zz=[]
while z:
     k=1
     while z[:k]==k*z[0]:
         k+=1
     zz+=[z[:k-1]]
     z=z[k-1:]

print zz
# -------------------------------


but I guess this code is not very idiomatic :(



More information about the Python-list mailing list