使用split将字符串拆分成列表,例如:
>>> '2 2 4 5 7'.split()
['2', '2', '4', '5', '7']
如您所见,元素是字符串。如果您想将元素作为整数,请使用 int 和列表推导:
>>> [int(elem) for elem in '2 2 4 5 7'.split()]
[2, 2, 4, 5, 7]
所以,在你的情况下,你会做这样的事情:
import sys
list_of_lists = []
for line in sys.stdin:
new_list = [int(elem) for elem in line.split()]
list_of_lists.append(new_list)
你最终会得到一个列表列表:
>>> list_of_lists
[[3], [2], [2, 2, 4, 5, 7]]
如果您想将这些列表作为变量,只需执行以下操作:
list1 = list_of_lists[0] # first list of this list of lists
list1 = list_of_lists[1] # second list of this list of lists
list1 = list_of_lists[2] # an so on ...