【发布时间】:2022-01-19 08:41:57
【问题描述】:
这个问题有点类似于this question,但不同之处在于它需要多个列表:
我有三个列表:
a = [1, 2, 3, 4, 5, 6, 7]
b = ['ball', 'cat', 'dog', 'elephant', 'baboon', 'crocodile']
c = [6, 3, 5, 4, 3, 2, 1]
我正在遍历列表如下:
for (x, y, z) in itertools.product(a, b, c):
print("The combination is: " + str(x) + ", " + y + ", " str(z))
我想添加一个用以下伪代码表示的条件:
for (x, y, z) in itertools.product(a, b, c):
if x != z:
print("The combination is: " + str(x) + ", " + y + ", " str(z))
if x == z:
skip to the next element in list "a" without skipping elements of "b" & "c" # this is pseudo code
我怎么能做到这一点? itertools 中是否有可以用来完成此任务的函数?
【问题讨论】:
-
可以用两个游标来完成循环。你坚持只在 itertools 中使用函数吗?
-
是的,我必须使用 itertools 来解决这个问题。
标签: python for-loop iteration itertools more-itertools