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))
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
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
> i was just curiuos about simple, clever way to write it in Python
It depends on what you mean by "clever".
For some, it was like a suggestion on using something already available such
as itertools.groupby, perhaps even better if it is actually compiled in from
a language like C and perhaps
On 6/9/2024 7:05 PM, avi.e.gr...@gmail.com wrote:
I remembered that HenHanna had been hard to deal with in the past and when
my reply to him/her/them bounced as a bad/fake address it came back to me
that I am better off not participating in this latest attempt to get us to
perform then probably
On 6/9/2024 3:50 PM, MRAB wrote:
On 2024-06-09 22:20, HenHanna via Python-list wrote:
Chunk, ChunkC -- nice simple way(s) to write these in Python?
(Chunk '(a a b a a a b b))
==> ((a a) (b) (a a a) (b b))
(Chunk '(a a a a b c c a a d e e e e))
==> ((a a a a)
I remembered that HenHanna had been hard to deal with in the past and when
my reply to him/her/them bounced as a bad/fake address it came back to me
that I am better off not participating in this latest attempt to get us to
perform then probably shoot whatever we say down.
A considerate person
HH,
Before bothering, it might be helpful if you spelled out a bit more in the
way of requirements that would satisfy you and perhaps show your attempts.
Your examples suggest it would be fairly simple to create an iterator, for
example, that would yield as it examined one item at a time until
On 2024-06-09 22:20, HenHanna via Python-list wrote:
Chunk, ChunkC -- nice simple way(s) to write these in Python?
(Chunk '(a a ba a a b b))
==> ((a a) (b) (a a a) (b b))
(Chunk '(a a a a b c c a a d e e e e))
==> ((a a a a) (b) (c c) (a a) (d) (e e e e))