nltk如何给出多个分句

2024-05-07 22:56:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有英文的句子列表(每句话都是一个列表),我想去拿英文的。 例如:

sentences = [['this', 'is', 'sentence', 'one'], ['hello','again']]

为了跑

nltk.utils.ngram

我需要把清单整理成:

sentences = ['this','is','sentence','one','hello','again']

但后来我发现了一个错误

('one','hello')

是的。 最好的处理方法是什么

谢谢


Tags: 方法hello列表is错误sentencesutilsthis
2条回答

你也可以使用列表理解

f = []
[f.extend(_l) for _l in sentences]

f = ['this', 'is', 'sentence', 'one', 'hello', 'again']

试试这个:

from itertools import chain

sentences = list(chain(*sentences))

chain返回一个chain对象,其.__next__()方法返回第一个iterable中的元素,直到它用尽为止,然后返回下一个iterable中的元素 无法忍受,直到所有无法忍受的人都筋疲力尽

或者你可以:

 sentences = [i for s in sentences for i in s]

相关问题 更多 >