Python 2.7 如何获取NLP同义词

Python 2.7 如何获取NLP同义词,python-2.7,nlp,nltk,wordnet,Python 2.7,Nlp,Nltk,Wordnet,我有一个获取同义词的片段(我从一篇文章中获得)。我想在列表中获取同义词,而不是像下面所示的那样打印出来 from nltk.corpus import wordnet as wn import nltk from nltk.corpus.reader.plaintext import PlaintextCorpusReader def me(): T = [] for i,j in enumerate(wn.synsets('small')): #print "

我有一个获取同义词的片段(我从一篇文章中获得)。我想在列表中获取同义词,而不是像下面所示的那样打印出来

from nltk.corpus import wordnet as wn
import nltk
from nltk.corpus.reader.plaintext import PlaintextCorpusReader

def me():
    T = []
    for i,j in enumerate(wn.synsets('small')):
        #print "Synonyms:", ", ".join(j.lemma_names())
        for item in [", ".join(j.lemma_names())]:
            print T.append(item)
如果我使用:
打印项目
, 我得到的答案是:

small
small
small, little
minor, modest, small, small-scale, pocket-size, pocket-sized
little, small
small
humble, low, lowly, modest, small
little, minuscule, small
little, small
small
modest, small
belittled, diminished, small
small
如果我使用
打印T.append(项目)
, 我明白了:

None
None
None
None
None
None
None
None
None
None
None
None
None
我想要的是:

[ small, little, minor, modest, small-scale, pocket-size, pocket-sized, humble, low, lowly, minuscule, belittled, diminished] 
T.append(item)
item
追加到列表中,并返回
None
。如果我理解正确,您希望看到列表不断增长(对吗?)。那么你可以试试这个:

for item in [", ".join(j.lemma_names())]:
    T.append(item)
    print T
或者(也许更好)
在方法末尾返回T
,并按如下方式使用:

T = me()
print T