permuting over nested dicts?

Paddy paddy3118 at googlemail.com
Wed Oct 31 14:04:13 EDT 2007


On Oct 31, 5:21 pm, Christian Meesters <meest... at uni-mainz.de> wrote:
> Hoi,
>
> I have the following data structure (of variable size actually, to make
> things simple, just that one):
> d = {'a': {'x':[1,2,3], 'y':[4,5,6]},
>      'b': {'x':[7,8,9], 'y':[10,11,12]}}
> This can be read as a dict of possibilities: The entities 'a' and 'b' have
> the parameters 'x' and 'y', each. And d['a']['x'] can be either 1 or 2 or
> 3. Does anybody know a convenient (and fast) way to permute over all
> possible nested dicts like
> {'a': {'x':1, 'y':4},
>  'b': {'x':7, 'y':10}}
> and
> {'a': {'x':2, 'y':4},
>  'b': {'x':7, 'y':10}}
> and so forth?
>
> Any link or snippet is appreciated.
>
> TIA
> Christian

from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/502199
import comb2
# (I wish)!

for xa,ya,xb,yb in comb2([1,2,3], [4,5,6], [7,8,9], [10,11,12]):
    print {'a': {'x': xa, 'y': ya},
           'b': {'x': xb, 'y': yb}}

Should work although untested.

- Paddy.




More information about the Python-list mailing list