繁体   English   中英

如何从python中的STDIN中读取多个列表?

[英]How to read multiple lists from STDIN in python?

我是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)

您必须指定* 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
>>> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM