Reputation: 405
For example, I have this list :
word1 = ['organization', 'community']
And I have a function to get a synonyms from the words of the list :
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)
and here's the output :
[
['organization', 'organisation', 'arrangement', 'system', 'administration',
'governance', 'governing body', 'establishment', 'brass', 'constitution',
'formation'],
['community', 'community of interests', 'residential district',
'residential area', 'biotic community']
]
^ the output above shows that each synsets for both 'organization'
and 'community'
are sublisted inside a list. and then I reduce the level of list :
newlist1 = [val for sublist in syn1 for val in sublist]
and here's the output :
['organization', 'organisation', 'arrangement', 'system', 'administration',
'governance', 'governing body', 'establishment', 'brass', 'constitution',
'formation', 'community', 'community of interests', 'residential district',
'residential area', 'biotic community']
^ now all the synsets remain the same strings without sublist. and what I'm trying to do now is to make all the synsets in newlist1
to be sublisted each other. I expect the output would be like this :
[['organization'], ['organisation'], ['arrangement'], ['system'],
['administration'], ['governance'], ['governing body'], ['establishment'],
['brass'], ['constitution'], ['formation'], ['community'],
['community of interests'], ['residential district'], ['residential area'],
['biotic community']]
I'm trying this code :
uplist1 = [[] for x in syn1]
uplist1.extend(syn1)
print(uplist1)
but the results is not what I expected:
[[], [],
['organization', 'organisation', 'arrangement', 'system', 'administration',
'governance', 'governing body', 'establishment', 'brass', 'constitution',
'formation'],
['community', 'community of interests', 'residential district',
'residential area', 'biotic community']]
It shows two empty lists and two lists of synsets for both 'organization'
and 'community'
How to make each strings of synsets a sublist?
Upvotes: 1
Views: 1581
Reputation: 2191
Take each element in syn1
and wrap in []
to make it a sublist.
Either with append
:
uplist1 = []
for i in syn1:
uplist1.append([i])
Or with the list comprehension equivalent:
uplist1 = [[i] for i in syn1]
Upvotes: 3
Reputation: 405
Thanks to Meow
's answer above who gave me inspiration. I came up with this:
upuplist1 = []
for i in newlist1:
upuplist1.append([i])
print(upuplist1)
Here's the output what I expected :
[[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']]
Upvotes: 0
Reputation: 22953
Simply use a list comprehension and wrap each item in a list:
>>> 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']]
Upvotes: 0
Reputation: 5613
You need to add brackets []
to val
in your list-comprehension to put each string
in a list
when creating newlist1
, for example:
newlist1 = [[val] for sublist in syn1 for val in sublist]
Upvotes: 1