将NLTK LazySubsequence转换为列表

时间:2015-09-30 20:29:40

标签: python nltk

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

E.g。

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列表:

from nltk.corpus import brown

def convert_LazySubsequence_to_list(LS):
    corpus = []
    for s in LS:
        corpus.append(s)
    return corpus

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

my_list = convert_LazySubsequence_to_list(LS)
print('type(my_list): {0}'.format(type(my_list)))

输出

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

1 个答案:

答案 0 :(得分:3)

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

>>> 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()的蟒蛇可以为你做到。