<p>你可以这样做:</p>
<pre><code>from itertools import groupby
from operator import itemgetter
output = [('the', 'B', 'NNP'), ('wall', 'I', 'NNP'), ('street', 'I', 'NNP'), ('journal', 'I', 'NNP'),
('reported', 'O', 'VB'), ('today', 'O', 'NNP'), ('that', 'O', 'NNP'), ('apple', 'B', 'NNP'),
('corporation', 'I', 'NNP'), ('made', 'O', 'VB'), ('money', 'O', 'NNP'), ('.', 'O', '.'),
('georgia', 'B', 'NNP'), ('tech', 'I', 'NNP'), ('is', 'O', 'NNP'), ('a', 'O', '.'),
('university', 'O', 'NNP'), ('in', 'O', 'NNP'), ('georgia', 'B', 'NNP'), ('.', 'O', '.')]
sentences = [list(group) for k, group in groupby(output, lambda x: x[0] == ".") if not k]
result = [tuple(map(list, zip(*map(itemgetter(0, 1), sentence)))) for sentence in sentences]
print(result)
</code></pre>
<p><strong>输出</strong></p>
<pre><code>[(['the', 'wall', 'street', 'journal', 'reported', 'today', 'that', 'apple', 'corporation', 'made', 'money'], ['B', 'I', 'I', 'I', 'O', 'O', 'O', 'B', 'I', 'O', 'O']), (['georgia', 'tech', 'is', 'a', 'university', 'in', 'georgia'], ['B', 'I', 'O', 'O', 'O', 'O', 'B'])]
</code></pre>
<p><strong>解释</strong></p>
<p>据我所知,你想把每个句子的第一个和最后一个元素拆开。你知道吗</p>
<p>线路:</p>
<pre><code>sentences = [list(group) for k, group in groupby(output, lambda x: x[0] == ".") if not k]
</code></pre>
<p>将<code>output</code>按每个<code>.</code>拆分成句子,第二行只是将每个句子解包:</p>
<pre><code>result = [tuple(map(list, zip(*map(itemgetter(0, 1), sentence)))) for sentence in sentences]
</code></pre>
<p>当你想要一个列表的元组列表,<a href="https://docs.python.org/3/library/functions.html#zip" rel="nofollow noreferrer">zip</a>返回一个元组列表时,你必须用list映射每个元组,然后将映射的结果转换成一个元组。你知道吗</p>