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

Liste des GroupesRevenir à cl python 
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.python
Date : 12. Jun 2024, 00:07:34
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v4ahr6$182gf$3@dont-email.me>
References : 1 2 3
User-Agent : Mozilla Thunderbird
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='aaabbccccaa'
print(chunk1(s))
print(chunk2(s))
###################################
Program output:
['aaa', 'bb', 'cccc', 'aa']
[('a', 3), ('b', 2), ('c', 4), ('a', 2)]
 Rob Cliffe
 
thank you...   OMG... For 10 minutes... i was SO mystified by
the question...
                 How can this code work??? ,  when it's
                   > def chunk1(seq):
                      and it's  [s]   within the def-body ?
it seemed as if the Compiler was doing a DWIM (Do what i mean)  trick.

On 09/06/2024 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))
 

Date Sujet#  Auteur
9 Jun 24 * in Python? -- Chunk -- (ChunkC '(a a b b b)), ==> ((a 2) (b 3))11HenHanna
10 Jun 24 +* Re: in Python? -- Chunk -- (ChunkC '(a a b b b)), ==> ((a 2) (b 3))2MRAB
10 Jun 24 i`- Re: in Python? -- Chunk -- (ChunkC '(a a b b b)), ==> ((a 2) (b 3))1HenHanna
10 Jun 24 +- Re: in Python? -- Chunk -- (ChunkC '(a a b b b)), ==> ((a 2) (b 3))1<avi.e.gross
10 Jun 24 +* Re: in Python? -- Chunk -- (ChunkC '(a a b b b)), ==> ((a 2) (b 3))4<avi.e.gross
10 Jun 24 i`* Re: in Python? -- Chunk -- (ChunkC '(a a b b b)), ==> ((a 2) (b 3))3HenHanna
10 Jun 24 i +- Re: in Python? -- Chunk -- (ChunkC '(a a b b b)), ==> ((a 2) (b 3))1<avi.e.gross
15 Jun 24 i `- Re: in Python? -- Chunk -- (ChunkC '(a a b b b)), ==> ((a 2) (b 3))1B. Pym
10 Jun 24 +* Re: in Python? -- Chunk -- (ChunkC '(a a b b b)), ==> ((a 2) (b 3))2Rob Cliffe
12 Jun 24 i`- Re: in Python? -- Chunk -- (ChunkC '(a a b b b)), ==> ((a 2) (b 3))1HenHanna
11 Jun 24 `- Re: in Python? -- Chunk -- (ChunkC '(a a b b b)), ==> ((a 2) (b 3))1<avi.e.gross

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal