1 个回答|356 次阅读
解包元组的时候,变量数应该和元组项数一致,如果变量数小于元组项数,可以向变量名添加星号*,这样这些值就将作为列表赋值给带星号的变量:
将其余值分配为名为“z”的列表:
fruits = ("apple", "banana", "cherry", "strawberry", "raspberry")
(x, y, *z) = fruits
print(x)
print(y)
print(z)
执行结果:
apple
banana
['cherry', 'strawberry', 'raspberry']
请先 登录 后评论