【发布时间】:2018-12-27 18:57:42
【问题描述】:
这可能很简单,但我卡了很长时间。
我试图迭代两种组合。但它并没有遍历所有项目。
itt_1 = [1, 2, 3]
comb_1 = combinations(itt, 2)
itt_2 = ['a', 'b', 'c']
comb_2 = combinations(itt_2, 2)
count = 0
for ii in list(comb_1):
for jj in list(comb_2):
print ii, jj
我预计会看到 9 个打印输出结果。但是,无论我是否使用列表功能,它都只显示前 3 个,见下文:
(1, 2) ('a', 'b')
(1, 2) ('a', 'c')
(1, 2) ('b', 'c')
我相信这与组合有关,因为它是用于迭代的生成器,并且只能使用一次。这是否意味着它不能用于嵌套的 for 循环?为什么上例中只打印comb_1的第一个组合?
【问题讨论】:
-
相关(可能是骗人的):stackoverflow.com/questions/44420135/…
标签: python-2.7 iterator