for c in [3,6]:
for s in [1,2]:
for d in [1,2,3]:
for z in [2,4,6,8]:
for x in ['B','C','D','E','F']:
for y in ['B','C','D','E','F','G']:
print(y,x,z,d,s,c)
如果我运行它,它将不会产生所有可能的结果。让我解释: 最后一个数字(对于[3,6]中的c)有2个可能的结果.. a 3或6.但是程序只打印3个。
答案 0 :(得分:1)
虽然您的代码应该可行,但如果您或您的计算机在处理这些深层嵌套循环时遇到问题,请尝试使用product()
中的itertools
,这样可以简化以下情况:
from itertools import product
c = [3, 6]
s = [1, 2]
d = [1, 2, 3]
z = [2, 4, 6, 8]
x = ['B','C','D','E','F']
y = ['B','C','D','E','F','G']
for sequence in product(y, x, z, d, s, c):
print(*sequence)
PARTIAL OUTPUT
B B 2 1 1 3
B B 2 1 1 6
B B 2 1 2 3
B B 2 1 2 6
B B 2 2 1 3
...
G F 8 2 2 6
G F 8 3 1 3
G F 8 3 1 6
G F 8 3 2 3
G F 8 3 2 6