Cho hai list l1 và l2. Tìm tích Descartes: tập hợp tất cả cặp có thứ tự (a, b) với a ∈ l1, b ∈ l2. Dùng itertools.product.
Ví dụ: l1 = [1,5,6], l2 = [1,2,9] → [(1,1), (1,2), (1,9), (5,1), (5,2), (5,9), (6,1), (6,2), (6,9)].
Ví dụ mã nguồn
from itertools import product
def solve(l1, l2):
return list(product(l1, l2))
l1 = [1,5,6]
l2 = [1,2,9]
print(solve(l1, l2))
Đầu vào / Đầu ra
Input: [1,5,6], [1,2,9]
Output: [(1, 1), (1, 2), (1, 9), (5, 1), (5, 2), (5, 9), (6, 1), (6, 2), (6, 9)]