[Tutor] Calculating and returning possible combinations of elements from a given set

Dave Angel davea at ieee.org
Wed Jul 28 04:11:25 CEST 2010



ZUXOXUS wrote:
> Oh, I think i got it:
>
>   
>>>> for prod in itertools.product('ABC', 'ABC'):
>>>>         
> print(prod)
>
> ('A', 'A')
> ('A', 'B')
> ('A', 'C')
> ('B', 'A')
> ('B', 'B')
> ('B', 'C')
> ('C', 'A')
> ('C', 'B')
> ('C', 'C')
>
> Thank you very much!!
>
> 2010/7/28 ZUXOXUS <zuxoxus at gmail.com>
>   
>
You're top-posting, which loses all the context. In this forum, put your 
comments after whatever lines you're quoting.

Your latest version gets the product of two.  But if you want an 
arbitrary number, instead of repeating the iterable ('ABC' in your 
case), you can use a repeat count.  That's presumably what you were 
trying to do in your earlier incantation.  But you forgot the 'repeat' 
keyword:

for prod in itertools.product('ABC', repeat=4):
    xxxx

will give you all the four-tuples.

DaveA






More information about the Tutor mailing list