in python, how do i iterate a nested dict with a dynamic number of nests?

混江龙づ霸主 提交于 2019-12-04 01:41:19

You need itertools.product.

from itertools import product

for vals in product(*list(aDict.values())):
    # vals will be (l1, l2, ..., ln) tuple

Same idea as DrTyrsa, but making sure order is right.

from itertools import product

for vals in product( *[aDict[i] for i in sorted(aDict.keys())]):
    print vals
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!