转换为子序列tk

2024-04-19 09:21:39 发布

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

是否有任何NLTK内置函数将NLTK LazySubsequence转换为列表?在

例如

from nltk.corpus import brown
corpus = brown.sents()
LS = corpus[0:250]
print('type(corpus)[0:250]: {0}'.format(type(LS)))

返回type(corpus)[0:250]: <class 'nltk.util.LazySubsequence'>。在

我知道我可以用convert_LazySubsequence_to_list()来转换循环,但我想知道NLTK是否提供了任何本机方法来转换为Python列表:

^{pr2}$

输出

type(corpus)[0:250]: <class 'nltk.util.LazySubsequence'>
type(my_list): <type 'list'>

Tags: 函数fromimport列表typeutilcorpusls
1条回答
网友
1楼 · 发布于 2024-04-19 09:21:39

实际上这比你想象的要容易

>>> from nltk.corpus import brown
>>> corpus = brown.sents()
>>> LS = corpus[0:250]
>>> print('type(corpus)[0:250]: {0}'.format(type(LS)))
type(corpus)[0:250]: <class 'nltk.util.LazySubsequence'>
>>> my_list = list(LS)
>>> print('type(my_list): {0}'.format(type(my_list)))
type(my_list): <class 'list'>

不需要任何特殊的NLTK函数。内置于list()的Python可以帮你完成。在

相关问题 更多 >