我是python的新手,正在尝试了解itertools.product。我无法从输入中读取多个列表。
最初,我已经给出了如下的手动输入。
list1 = [1,2]
list2 = [3,4]
print(*product(list1, list2))
并输出为(1, 3) (1, 4) (2, 3) (2, 4)
,这很好。
我希望同一件事能在产品功能中使用多个列表。
我尝试过以下方式
TotList = product(list(map(int,input().split())) for _ in range(2)) #in range function 2 can be vary
for item in TotList:
print(*item)
但是它不能像产品工具一样工作
当前输入:
1 2
3 4
输出:
[1, 2]
[3, 4]
预期输出:
(1, 3) (1, 4) (2, 3) (2, 4)
答案 0 :(得分:3)
您必须指定* operator
来解开map生成的可迭代对象并将其提供给product
>>> TotList = product(*(map(int,input().split()) for _ in range(2)))
1 2
3 4
>>> for item in TotList:
... print(*item)
...
1 3
1 4
2 3
2 4
>>>