Generating list of possible configurations

Terry Reedy tjreedy at udel.edu
Thu Jul 3 02:40:52 CEST 2008



Mensanator wrote:
> On Jul 2, 4:53 pm, "bjorklund.e... at gmail.com"
> <bjorklund.e... at gmail.com> wrote:

>> After this I tried figuring out a function that would generate the
>> different possible configurations, but I couldn't quite wrap my head
>> around it...

> Lookup "Cartesian Product".
> 
>> Any pointers as to how one would go about
>> solving something like this would be greatly appreciated.
> 
> for a in [True,False]:
>   for b in [True,False]:
>     for c in [1,2,3,4]:
>       print 'combined settings:',a,'\t',b,'\t',c

This has been added to itertools at least for 2.6/3.0

 >>> import itertools as it
 >>> for prod in it.product((True,False), (True,False), (1,2,3,4)):
	print(prod) # or run test

(True, True, 1)
(True, True, 2)
(True, True, 3)
(True, True, 4)
(True, False, 1)
(True, False, 2)
(True, False, 3)
(True, False, 4)
(False, True, 1)
(False, True, 2)
(False, True, 3)
(False, True, 4)
(False, False, 1)
(False, False, 2)
(False, False, 3)
(False, False, 4)

The sequences of sequences can, of course, be a variable:

 >>> options = ((True,False), (True,False), (1,2,3,4))
 >>> for prod in it.product(*options): print(prod)

does the same thing.  So you can change 'options' without changing the 
test runner.

tjr




More information about the Python-list mailing list