1

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?

4 Answers 4

3

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]
Sign up to request clarification or add additional context in comments.

1 Comment

I'm sorry sir, not like that. [[[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']]] << it appends a list for synset group but not for each strings
1

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]

2 Comments

@ChristianDean why do that when it can be done when creating newlist1?
Ah, my apologizes. I didn't realize you were also flattening syn1 and not just iterating over it.
0

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']]

Comments

0

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']]

1 Comment

I implement it for list 'newlist1'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.