Generating list of possible configurations

Mensanator mensanator at aol.com
Wed Jul 2 18:21:00 EDT 2008


On Jul 2, 4:53 pm, "bjorklund.e... at gmail.com"
<bjorklund.e... at gmail.com> wrote:
> Hello pythonistas.
>
> I'm a newbie to pretty much both programming and Python. I have a task
> that involves writing a test script for every possible combination of
> preference settings for a software I'm testing. I figured that this
> was something that a script could probably do pretty easily, given all
> the various possibilites.
>
> I started creating a dictionary of all the settings, where each key
> has a value that is a list of the possible values for that setting.
> Most of the settings are simple booleans (setting is on or off), some
> of them are drop-downs with several values. For example:
>
> settings = {
>     'setting_a': (True, False),
>     'setting_b': (True, False),
>     'setting_c': (1, 2, 3, 4),
>
> }
>
> 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...

Basically, for each setting of a, you must do each possible b,
and for each a,b setting you must do each possible c, etc.

> Are there any general patterns/structures that are suited
> for this type of task?

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

combined settings: True 	True 	1
combined settings: True 	True 	2
combined settings: True 	True 	3
combined settings: True 	True 	4
combined settings: True 	False 	1
combined settings: True 	False 	2
combined settings: True 	False 	3
combined settings: True 	False 	4
combined settings: False 	True 	1
combined settings: False 	True 	2
combined settings: False 	True 	3
combined settings: False 	True 	4
combined settings: False 	False 	1
combined settings: False 	False 	2
combined settings: False 	False 	3
combined settings: False 	False 	4


> It's not
> that I really need to use Python for it, but I thought it could be a
> good learning excercise... :-)

You may, then, also be interested in

Permutations with    Replacement
Permutations without Replacement
Combinations with    Replacement
Combinations without Replacement

>
> Kind regards,
> //Emil




More information about the Python-list mailing list