嵌套在Python中的循环


问题内容

我想做类似的事情

for a in [0..1]:
    for b in [0..1]:
        for c in [0..1]:
            do something

但是,我可能有15个不同的变量。有没有更简单的方法,例如

for a, b, c in [0..1]:
    do something

谢谢你的帮助


问题答案:

itertools.product:

import itertools
for a,b,c in itertools.product([0, 1], repeat=3):
  # do something