Re: in Python? -- Chunk -- (ChunkC '(a a b b b)), ==> ((a 2) (b 3))

2024-06-11 Thread HenHanna via Python-list
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))

RE: in Python? -- Chunk -- (ChunkC '(a a b b b)), ==> ((a 2) (b 3))

2024-06-11 Thread AVI GROSS via Python-list
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

Re: in Python? -- Chunk -- (ChunkC '(a a b b b)), ==> ((a 2) (b 3))

2024-06-11 Thread Rob Cliffe via Python-list
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