Bar*_*ski 7 python iterable-unpacking
我想知道这是否可能:
def someFunction():
return list(range(5))
first, rest = someFunction()
print(first) # 0
print(rest) # [1,2,3,4]
Run Code Online (Sandbox Code Playgroud)
我知道可以通过以下3行完成:
result = someFunction()
first = result[0]
rest = result[1:]
Run Code Online (Sandbox Code Playgroud)
the*_*eye 20
如果您使用的是Python 3.x,则可以执行此操作
first, *rest = someFunction()
print (first, rest)
Run Code Online (Sandbox Code Playgroud)
在本PEP中阅读更多相关信息
在Python 2中,您可以做的最好的事情是
result = someFunction()
first, rest = result[0], result[1:]
Run Code Online (Sandbox Code Playgroud)