Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

Living your life is a task so difficult, it has never been attempted before.


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

SubjectAuthor
* Cprod -- (writing this: itertools.product([0, 1], repeat=N )HenHanna
`* Re: Cprod -- (writing this: itertools.product([0, 1], repeat=N )dn
 `- Re: Cprod -- (writing this: itertools.product([0, 1], repeat=N )HenHanna

1
Subject: Cprod -- (writing this: itertools.product([0, 1], repeat=N )
From: HenHanna
Newsgroups: comp.lang.python
Organization: A noiseless patient Spider
Date: Tue, 21 May 2024 19:14 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: HenHanna@devnull.tb (HenHanna)
Newsgroups: comp.lang.python
Subject: Cprod -- (writing this: itertools.product([0, 1], repeat=N )
Date: Tue, 21 May 2024 12:14:03 -0700
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <v2irpr$ndjr$3@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 21 May 2024 21:14:04 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="4ee11fe942d496548c5fef5d03dc9b69";
logging-data="767611"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/LiFwHl3rJAVqqsrbbVp5GKeRoDaiW1+g="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:udURA4DPPneXVWixhhFioRgb1Uc=
Content-Language: en-US
View all headers

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?

Subject: Re: Cprod -- (writing this: itertools.product([0, 1], repeat=N )
From: dn
Newsgroups: comp.lang.python
Organization: DWM
Date: Tue, 21 May 2024 22:44 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: PythonList@DancesWithMice.info (dn)
Newsgroups: comp.lang.python
Subject: Re: Cprod -- (writing this: itertools.product([0, 1], repeat=N )
Date: Wed, 22 May 2024 10:44:44 +1200
Organization: DWM
Lines: 31
Message-ID: <mailman.44.1716331948.2909.python-list@python.org>
References: <v2irpr$ndjr$3@dont-email.me>
<c072d9d7-1055-4cd6-ad94-64dea4a0bdd9@DancesWithMice.info>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de 4NW3E8RiJm13nWUQFkCDlwlQz5BkC9qN223lxFjRR+qg==
Cancel-Lock: sha1:MSKBJnq3hBr3ewQ/aXUpbMwB4vw= sha256:8cs9/UIEwbtzFvTSe6bvaw/TMt/50qgoHKNUzNSsHUQ=
Return-Path: <PythonList@DancesWithMice.info>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=danceswithmice.info header.i=@danceswithmice.info
header.b=bMux84l4; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.004
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'def': 0.04; '???': 0.07;
'skip:\xc2 30': 0.07; '=dn': 0.09;
'from:addr:danceswithmice.info': 0.09; 'from:addr:pythonlist':
0.09; 'received:192.168.1.64': 0.09; 'skip:\xc2 20': 0.09;
'subject:writing': 0.09; 'url:stable': 0.09; 'message-
id:@DancesWithMice.info': 0.16; 'received:cloud': 0.16;
'received:rangi.cloud': 0.16; 'wrote:': 0.16; 'it?': 0.19;
'to:addr:python-list': 0.20; 'function': 0.27; 'header:User-
Agent:1': 0.30; 'header:Organization:1': 0.31; 'python-list':
0.32; 'received:192.168.1': 0.32; 'there': 0.33; '----------':
0.33; 'header:In-Reply-To:1': 0.34; 'url-ip:104.17/16': 0.35;
'received:192.168': 0.37; 'way': 0.38; 'list': 0.39; 'skip:\xc2
10': 0.62; 'skip:i 20': 0.62; 'url:product': 0.64; 'improve':
0.66; 'subject:this': 0.67; 'subject:skip:i 10': 0.69;
'8bit%:100': 0.76; 'subject: )': 0.84
DKIM-Filter: OpenDKIM Filter v2.11.0 vps.rangi.cloud 53C1C3F95
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=danceswithmice.info;
s=staff; t=1716331490;
bh=NOOgjyH89mEwlws7ySzy7igxAQJ5ALtFckO5L1ezQR4=;
h=Date:From:Subject:To:References:In-Reply-To:From;
b=bMux84l4UchSbLYBO+wtIuLv0SyckurbF0QRT3HhoR/0VY8Jr+C+ie/i+MTnYiPy3
cYYHLbwHZRFqZWg18UtprJMMKX1Wl1/YvSs80gxfsoutFRx6Cfzzr9m0OUfj2qAULU
jlczm7kXUqyiBIy3aw5rEQOXEuErEU+ZeeL6257ci2r6Qwr4ZW+gK9MFjE3CbxblJY
kJ3+M0QUOp5N5R7x6Mlt7WJcH0WbU9bWCaYeEf+zkbR4FSq3WTDc8dNoPo2qBzgxsX
XlHixvF4qbiJFpnsXDp0oE9Ovdne1X7R3fXz8fX7CHmeF7Mu2PxyW5cQAiqyH98GzP
gpSZhhezKi/Xg==
User-Agent: Mozilla Thunderbird
Content-Language: en-US
In-Reply-To: <v2irpr$ndjr$3@dont-email.me>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <c072d9d7-1055-4cd6-ad94-64dea4a0bdd9@DancesWithMice.info>
X-Mailman-Original-References: <v2irpr$ndjr$3@dont-email.me>
View all headers

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

Subject: Re: Cprod -- (writing this: itertools.product([0, 1], repeat=N )
From: HenHanna
Newsgroups: comp.lang.python
Organization: novaBBS
Date: Wed, 22 May 2024 05:21 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!i2pn.org!i2pn2.org!.POSTED!not-for-mail
From: HenHanna@dev.null (HenHanna)
Newsgroups: comp.lang.python
Subject: Re: Cprod -- (writing this: itertools.product([0, 1],
repeat=N )
Date: Wed, 22 May 2024 05:21:53 +0000
Organization: novaBBS
Message-ID: <fc0c8ed06e0800857ed913f5e128c2fa@www.novabbs.com>
References: <v2irpr$ndjr$3@dont-email.me> <c072d9d7-1055-4cd6-ad94-64dea4a0bdd9@DancesWithMice.info> <mailman.44.1716331948.2909.python-list@python.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: i2pn2.org;
logging-data="1842298"; mail-complaints-to="usenet@i2pn2.org";
posting-account="25PjXUQKTQXKZnoxTqVufZcfCkmLjnu8AjjfHtuMysE";
User-Agent: Rocksolid Light
X-Face: P#KeQ)CUdd!==@fw~Ms1=,Hb`IWtb6:Mw)x3B=H1BfNC\lz?Nb&)M9}$>?'X7l;CuB}utlJ=PHsRBSG6X>dYZ$[>P]$~+`>@V6$t}hTLoQ7XC~W\>:`B3ALU]SH;d(\MEc}znW8m}-ma&yPFkJ2@KSQrz=!Y;><;6a>z6N+mt`ClCt.PAE<o+B$qjwejZSZ,w]^;vrdl24z5(pm={l,F10qRDF
X-Rslight-Site: $2y$10$eCqKGnn1EtrHprIOCXIr6uSDsmtxMV/cDowWyGsUSvV73MdJCmAP.
X-Spam-Checker-Version: SpamAssassin 4.0.0
X-Rslight-Posting-User: 5a1f1f09909a70d7ae18ae9af00e018f83ece577
View all headers

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.

1

rocksolid light 0.9.8
clearnet tor