Guest User

Untitled

a guest
Aug 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.47 KB | None | 0 0
  1. in python, how do i iterate a nested dict with a dynamic number of nests?
  2. aDict[1]=[1,2,3]
  3. aDict[2]=[7,8,9,10]
  4. aDict[n]=[x,y]
  5.  
  6. for l1 in aDict[1]:
  7. for l2 in aDict[2]:
  8. for ln in aDict[n]:
  9. # do stuff with l1, l2, ln combination.
  10.  
  11. from itertools import product
  12.  
  13. for vals in product(*list(aDict.values())):
  14. # vals will be (l1, l2, ..., ln) tuple
  15.  
  16. from itertools import product
  17.  
  18. for vals in product( *[aDict[i] for i in sorted(aDict.keys())]):
  19. print vals
Add Comment
Please, Sign In to add comment