NUM1 NUM2 NUM3 x y z。。。
[num1, num2, num3, the_rest_of_the_string_without_splitting] = split(" ")
有没有办法一行一行的呢?
对。有个论点叫 maxsplit 您可以使用:
maxsplit
t = "NUM1 NUM2 NUM3 x y z" t.split(" ", maxsplit=3) # => ['NUM1', 'NUM2', 'NUM3', 'x y z']
From the docs:
如果给定maxslit,则最多完成maxslit split(因此,列表将最多包含maxslit+1个元素)。