Python nltk如何给出多个独立的句子
我有英语句子列表(每个句子都是一个列表),我想去拿ngrams。 例如:Python nltk如何给出多个独立的句子,python,list,nested,nested-lists,flatten,Python,List,Nested,Nested Lists,Flatten,我有英语句子列表(每个句子都是一个列表),我想去拿ngrams。 例如: sentences = [['this', 'is', 'sentence', 'one'], ['hello','again']] 为了跑 nltk.utils.ngram 我需要将列表平铺到: sentences = ['this','is','sentence','one','hello','again'] 但后来我发现了一个错误 (“一”,“你好”) 。 最好的处理方法是什么 谢谢 试试这个: from ite
sentences = [['this', 'is', 'sentence', 'one'], ['hello','again']]
为了跑
nltk.utils.ngram
我需要将列表平铺到:
sentences = ['this','is','sentence','one','hello','again']
但后来我发现了一个错误
(“一”,“你好”)
。
最好的处理方法是什么
谢谢 试试这个:
from itertools import chain
sentences = list(chain(*sentences))
chain
返回一个chain对象,该对象的方法返回第一个iterable中的元素,直到用完为止,然后返回下一个iterable中的元素
无法忍受,直到所有无法忍受的人都筋疲力尽
或者你可以:
sentences = [i for s in sentences for i in s]
您还可以使用列表理解
f = []
[f.extend(_l) for _l in sentences]
f = ['this', 'is', 'sentence', 'one', 'hello', 'again']
实际上需要连环(*句),效果很好,谢谢