如何为 python 列表中的每个字符串创建子列表?
How to make a sublists for each strings inside a list in python?
例如,我有这个列表:
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'
和 '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'
和 'community'
的两个空列表和两个同义词集列表
如何将同义词集的每个字符串都变成一个子列表?
取出 syn1
中的每个元素并包裹在 []
中使其成为子列表。
与append
:
uplist1 = []
for i in syn1:
uplist1.append([i])
或等效于列表理解:
uplist1 = [[i] for i in syn1]
感谢Meow
's 给了我灵感。我想到了这个:
upuplist1 = []
for i in newlist1:
upuplist1.append([i])
print(upuplist1)
这是我预期的输出:
[[u'organization'], [u'organisation'], [u'arrangement'], [u'system'], [u'administration'], [u'governance'], [u'governing body'], [u'establishment'], [u'brass'], [u'constitution'], [u'formation'], [u'community'], [u'community of interests'], [u'residential district'], [u'residential area'], [u'biotic community']]
您需要在列表理解中添加括号 []
到 val
,以便在创建 newlist1
时将每个 string
放入 list
,因为示例:
newlist1 = [[val] for sublist in syn1 for val in sublist]
只需使用列表理解并将每个项目包装在列表中:
>>> lst = [[el] for el in newlist1]
[['organization'],
['organisation'],
['arrangement'],
['system'],
['administration'],
['governance'],
['governing body'],
['establishment'],
['brass'],
['constitution'],
['formation'],
['community'],
['community of interests'],
['residential district'],
['residential area'],
['biotic community']]
例如,我有这个列表:
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'
和 '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'
和 'community'
如何将同义词集的每个字符串都变成一个子列表?
取出 syn1
中的每个元素并包裹在 []
中使其成为子列表。
与append
:
uplist1 = []
for i in syn1:
uplist1.append([i])
或等效于列表理解:
uplist1 = [[i] for i in syn1]
感谢Meow
's
upuplist1 = []
for i in newlist1:
upuplist1.append([i])
print(upuplist1)
这是我预期的输出:
[[u'organization'], [u'organisation'], [u'arrangement'], [u'system'], [u'administration'], [u'governance'], [u'governing body'], [u'establishment'], [u'brass'], [u'constitution'], [u'formation'], [u'community'], [u'community of interests'], [u'residential district'], [u'residential area'], [u'biotic community']]
您需要在列表理解中添加括号 []
到 val
,以便在创建 newlist1
时将每个 string
放入 list
,因为示例:
newlist1 = [[val] for sublist in syn1 for val in sublist]
只需使用列表理解并将每个项目包装在列表中:
>>> lst = [[el] for el in newlist1]
[['organization'],
['organisation'],
['arrangement'],
['system'],
['administration'],
['governance'],
['governing body'],
['establishment'],
['brass'],
['constitution'],
['formation'],
['community'],
['community of interests'],
['residential district'],
['residential area'],
['biotic community']]