例如,我有这个列表:
word1 = ['organization', 'community']
我有一个函数可以从列表中的单词中获取同义词:
from nltk.corpus import wordnet as wn
def getSynonyms(word1):
synonymList1 = []
for data1 in word1:
wordnetSynset1 = wn.synsets(data1)
tempList1 = []
for synset1 in wordnetSynset1:
synLemmas = synset1.lemma_names()
for s in synLemmas:
word = s.replace('_', ' ')
if word not in tempList1:
tempList1.append(word)
synonymList1.append(tempList1)
return synonymList1
syn1 = getSynonyms(word1)
print(syn1)
这是输出:
[
['organization', 'organisation', 'arrangement', 'system', 'administration',
'governance', 'governing body', 'establishment', 'brass', 'constitution',
'formation'],
['community', 'community of interests', 'residential district',
'residential area', 'biotic community']
]
^ 上面的输出显示两个的每个同义词集'organization'
and 'community'
在列表中列出。然后我减少列表的级别:
newlist1 = [val for sublist in syn1 for val in sublist]
这是输出:
['organization', 'organisation', 'arrangement', 'system', 'administration',
'governance', 'governing body', 'establishment', 'brass', 'constitution',
'formation', 'community', 'community of interests', 'residential district',
'residential area', 'biotic community']
^ 现在所有同义词集都保持相同的字符串,没有子列表。我现在想做的就是使所有同义词集newlist1
彼此进行子列表。我预计输出会是这样的:
[['organization'], ['organisation'], ['arrangement'], ['system'],
['administration'], ['governance'], ['governing body'], ['establishment'],
['brass'], ['constitution'], ['formation'], ['community'],
['community of interests'], ['residential district'], ['residential area'],
['biotic community']]
我正在尝试这段代码:
uplist1 = [[] for x in syn1]
uplist1.extend(syn1)
print(uplist1)
但结果并不是我所期望的:
[[], [],
['organization', 'organisation', 'arrangement', 'system', 'administration',
'governance', 'governing body', 'establishment', 'brass', 'constitution',
'formation'],
['community', 'community of interests', 'residential district',
'residential area', 'biotic community']]
它显示了两个空列表和两个同义词集列表'organization'
and 'community'
如何使同义词集的每个字符串成为一个子列表?