4 回答

TA贡献1735条经验 获得超5个赞
你可以itertools.product像这样使用:
list1 = [1,2,3,4]
list2 = [6,7,8]
find_this_value = (1, 8)
found_value = False
for permutation in itertools.product(list1, list2):
if permutation == find_this_value:
found_value = True
break
if found_value:
pass # Take action
itertools.product返回一个生成器,其中包含 2 个列表的所有可能排列。然后,您只需迭代这些,并搜索直到找到您想要的值。

TA贡献1806条经验 获得超5个赞
如果您不想itertools.product按照另一个答案中的建议使用,您可以将其包装在一个函数中并返回:
list1 = [1,2,3,4]
list2 = [6,7,8]
def findNumbers(x, y):
for i in list1:
for j in list2:
print(str(i) + ", " + str(j))
if (x, y) == (i, j):
return (x, y)
输出:
>>> findNumbers(2, 7)
1, 6
1, 7
1, 8
2, 6
2, 7
(2, 7)
添加回答
举报