4 回答

TA貢獻1735條經(jīng)驗 獲得超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條經(jīng)驗 獲得超5個贊
如果您不想itertools.product按照另一個答案中的建議使用,您可以將其包裝在一個函數(shù)中并返回:
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)
添加回答
舉報