Re: Cprod -- (writing this: itertools.product([0, 1], repeat=N )

Liste des GroupesRevenir à cl python 
Sujet : Re: Cprod -- (writing this: itertools.product([0, 1], repeat=N )
De : HenHanna (at) *nospam* dev.null (HenHanna)
Groupes : comp.lang.python
Date : 22. May 2024, 07:21:53
Autres entêtes
Organisation : novaBBS
Message-ID : <fc0c8ed06e0800857ed913f5e128c2fa@www.novabbs.com>
References : 1 2 3
User-Agent : Rocksolid Light
dn wrote:

On 22/05/24 07:14, HenHanna via Python-list wrote:
 How can i write this function Cprod (Cartesian Product) simply?
                  (writing this out: itertools.product([0, 1], repeat=N
)
 The value can be a list or a Tuple.
                  cprod([0, 1], 1) => ((0) (1))
                  cprod([0, 1], 2) => ((0,0) (0,1) (1,0) (1,1))
   This works:
     def cprod(x, c):
         if c==1: return [[i] for i in x]
         Sub= cprod(x, c-1)
         return [i  for F in x   for i in [[F]+R for R in Sub]]
  ---------- Is there another way to write [F]+R ???
                 Other ways to improve it?

https://python.readthedocs.io/en/stable/library/itertools.html#itertools.product
   Regards,  =dn
         Thank you...  That code looks elegant...  I'll study it.

Date Sujet#  Auteur
21 May 24 * Cprod -- (writing this: itertools.product([0, 1], repeat=N )3HenHanna
22 May 24 `* Re: Cprod -- (writing this: itertools.product([0, 1], repeat=N )2dn
22 May 24  `- Re: Cprod -- (writing this: itertools.product([0, 1], repeat=N )1HenHanna

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal