列表理解中元素的解包 "the rest" - python3to2

Unpacking "the rest" of the elements in list comprehension - python3to2

在 Python 3 中,我可以使用 for i, *items in tuple 将元组中的第一个时间和其余部分隔离为项目,例如:

>>> x = [(2, '_', 'loves', 'love', 'VBZ', 'VBZ', '_', '0', 'ROOT', '_', '_'), (1, '_', 'John', 'John', 'NNP', 'NNP', '_', '2', 'nsubj', '_', '_'), (3, '_', 'Mary', 'Mary', 'NNP', 'NNP', '_', '2', 'dobj', '_', '_'), (4, '_', '.', '.', '.', '.', '_', '2', 'punct', '_', '_')]
>>> [items for n, *items in sorted(x)]
[['_', 'John', 'John', 'NNP', 'NNP', '_', '2', 'nsubj', '_', '_'], ['_', 'loves', 'love', 'VBZ', 'VBZ', '_', '0', 'ROOT', '_', '_'], ['_', 'Mary', 'Mary', 'NNP', 'NNP', '_', '2', 'dobj', '_', '_'], ['_', '.', '.', '.', '.', '_', '2', 'punct', '_', '_']]

我需要将其反向移植到 Python 2 并且我不能使用 * 指针来收集元组中的其余项目。

只使用切片跳过第一个元素:

[all_items[1:] for all_items in sorted(x)]

语法参考extended tuple unpacking,其中*前缀的名称称为catch-all name.参见 PEP 3132。语法没有向后移植。