将两个变量分配给一个列表切片

beo*_*ver 7 python list pattern-matching slice

是否可以一次性分配给列表切片,这将实现以下目标:

mylist = [1,2,3,4,5,6,7]

xs = mylist[:-1]
x  = mylist[-1]

xs == [1,2,3,4,5,6]
x  == 7
Run Code Online (Sandbox Code Playgroud)

我知道我可以这样写:

xs,x = mylist[:-1], mylist[-1]
Run Code Online (Sandbox Code Playgroud)

但我想知道是否有可能以任何其他方式.或者被Haskell的模式匹配宠坏了.

就像是 x,xs = mylist[:funky:slice:method:]

sen*_*rle 11

你可以在Python 3中:

>>> *xs, x = [1, 2, 3, 4, 5, 6, 7]
>>> xs
[1, 2, 3, 4, 5, 6]
>>> x
7
Run Code Online (Sandbox Code Playgroud)