import itertools
def chunk1(seq):
return [ ch * len(list(grp)) for (ch, grp) in itertools.groupby(s) ]
def chunk2(seq):
return [ (ch, len(list(grp))) for (ch, grp) in itertools.groupby(s) ]
s='aaabbaa'
print(chunk1(s))
print(chunk2(s))
###
Program out
Rob,
That is a fairly straightforward and elegant solution if using an added mode
called itertools.
I went a different way by creating my own focused iterator and calling it, when
needed, to produce one or the other of the results requested in the functions
named chunk() and chunkc(). But note
On 6/10/2024 6:29 AM, Rob Cliffe wrote:
import itertools
def chunk1(seq):
return [ ch * len(list(grp)) for (ch, grp) in itertools.groupby(s) ]
def chunk2(seq):
return [ (ch, len(list(grp))) for (ch, grp) in itertools.groupby(s) ]
s='aaabbaa'
print(chunk1(s))
print(chunk2(s))