Hi, I'm quite new to python and am looking for help with lists inside lists
This is an example I'm trying to do. I have the following thesaurus set...
thesaurus = [
['item1', 'item2', 'item3', 'item4'],
['itema', itemb', 'itemc', 'itemd']
]
The real one for my assignment has many more lists inside lists, but I am trying to keep it simple to find the solution.
I need to be able to return each list out of the thesaurus if they have a word which matches to one or more of the words in the sublist.
For example if I have the words 'item1' and 'itemb', both the entire lists will be returned, but if I jsut have 'item2', the first list will be returned.
I have tried googleing, but I wasn't exactly sure what lists inside lists are called? Tried sublists, but couldn't find much. I can get the answer if there was only one list, but can't figure out how to select and return a single list from a group of lists.
This works for one list
thesaurus = ['itema', itemb', 'itemc', 'itemd']
def synonyms (search_word1):
if (search_word1) in thesaurus:
return thesaurus
For multiple lists in one group (the lists at the top of my post) I tried this:
def synonyms (search_word1, search_word2):
if (search_word1) or (search_word2) in thesaurus:
return ????certain list within thesaurus, don't know syntax.
Any help would be greatly appreciated!
(also I'm not sure why the indenting isn't showing when I submit this)
This is an example I'm trying to do. I have the following thesaurus set...
thesaurus = [
['item1', 'item2', 'item3', 'item4'],
['itema', itemb', 'itemc', 'itemd']
]
The real one for my assignment has many more lists inside lists, but I am trying to keep it simple to find the solution.
I need to be able to return each list out of the thesaurus if they have a word which matches to one or more of the words in the sublist.
For example if I have the words 'item1' and 'itemb', both the entire lists will be returned, but if I jsut have 'item2', the first list will be returned.
I have tried googleing, but I wasn't exactly sure what lists inside lists are called? Tried sublists, but couldn't find much. I can get the answer if there was only one list, but can't figure out how to select and return a single list from a group of lists.
This works for one list
thesaurus = ['itema', itemb', 'itemc', 'itemd']
def synonyms (search_word1):
if (search_word1) in thesaurus:
return thesaurus
For multiple lists in one group (the lists at the top of my post) I tried this:
def synonyms (search_word1, search_word2):
if (search_word1) or (search_word2) in thesaurus:
return ????certain list within thesaurus, don't know syntax.
Any help would be greatly appreciated!
(also I'm not sure why the indenting isn't showing when I submit this)
Comment