Sujet : Re: in Python? -- Chunk -- (ChunkC '(a a b b b)), ==> ((a 2) (b 3))
De : HenHanna (at) *nospam* devnull.tb (HenHanna)
Groupes : comp.lang.pythonDate : 10. Jun 2024, 01:16:27
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v45d4b$3s5s4$1@dont-email.me>
References : 1 2 3
User-Agent : Mozilla Thunderbird
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) (b) (c c) (a a) (d) (e e e e))
>
>
(Chunk '(2 2 foo bar bar j j j k baz baz))
==> ((2 2) (foo) (bar bar) (j j j) (k) (baz baz))
>
_________________
>
(ChunkC '(a a b b b))
==> ((a 2) (b 3))
>
(ChunkC '(a a b a a a b b))
==> ((a 2) (b 1) (a 3) (b 2))
You can make use of itertools.groupby.
Thanks! i'll try it.
Scheme (Gauche)
(use srfi-1) ; span
(define (gp x)
(if (null? x) '()
(let-values (((F L) (span (cut equal? (car x) <>) x)))
(cons F (gp L)))))
(print (gp '(a b b a a a b b b b)))
(print (gp '(c c c a d d d d a e e e e e)))