您可以使用operator.itemgetter
:
>>> from operator import itemgetter
>>> bar = ['a','b','c','x','y','z']
>>> itemgetter(0, 3, 4)(bar)
('a', 'x', 'y')
因此,对于您的示例,您将执行以下操作:
>>> v1, v2, v3 = itemgetter(0, 3, 4)(bar)
通过Python中的切片列表分配值的紧凑方法
您可以使用operator.itemgetter
:
>>> from operator import itemgetter
>>> bar = ['a','b','c','x','y','z']
>>> itemgetter(0, 3, 4)(bar)
('a', 'x', 'y')
因此,对于您的示例,您将执行以下操作:
>>> v1, v2, v3 = itemgetter(0, 3, 4)(bar)