By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,986 Members | 1,583 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,986 IT Pros & Developers. It's quick & easy.

Help withselecting and returning lists

P: 6
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)
Mar 26 '08 #1
Share this Question
Share on Google+
3 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
You need to iterate on the elements of thesaurus to see if word is in one of the elements. It's not very efficient with large lists.
Expand|Select|Wrap|Line Numbers
  1. thesaurus = [['item1', 'item2', 'item3', 'item4'], \
  2.              ['itema', 'itemb', 'itemc', 'itemd']]
  3.  
  4. def synonyms(search_word):
  5.     for item in thesaurus:
  6.         if search_word in item:
  7.             return item
  8.  
>>> print synonyms('item2')
['item1', 'item2', 'item3', 'item4']
>>>

A possible alternative would be a dictionary. The dictionary could have a key for each word in each sublist. The data would need more memory, but the lookup would be faster.
Expand|Select|Wrap|Line Numbers
  1. >>> 
  2. >>> dd = {}
  3. >>> for item in thesaurus:
  4. ...     for word in item:
  5. ...         dd[word]=item
  6. ...         
  7. >>> dd.get('item1', None)
  8. ['item1', 'item2', 'item3', 'item4']
  9. >>> print dd.get('some_word', None)
  10. None
  11. >>> for key in dd:
  12. ...     print '%s = %s' % (key, dd[key])
  13. ...     
  14. item2 = ['item1', 'item2', 'item3', 'item4']
  15. item3 = ['item1', 'item2', 'item3', 'item4']
  16. item1 = ['item1', 'item2', 'item3', 'item4']
  17. item4 = ['item1', 'item2', 'item3', 'item4']
  18. itemb = ['itema', 'itemb', 'itemc', 'itemd']
  19. itemc = ['itema', 'itemb', 'itemc', 'itemd']
  20. itema = ['itema', 'itemb', 'itemc', 'itemd']
  21. itemd = ['itema', 'itemb', 'itemc', 'itemd']
  22. >>>
I am sure there is a better way. HTH, bv
Mar 26 '08 #2

Expert 100+
P: 849
These would probably be called nested lists. A Python feature known as list comprehension can solve this in a line or two, believe it or not, or you can use a loop to iterate over each list and check each one for the item you're looking for.

To get your indenting back, use [code] tags.
Mar 26 '08 #3

P: 6
Thanks a lot, the first code worked fine, thanks a lot! I didn't understand all of the second part you posted, I think it's a little beyond me for now, only been going 4 weeks.
Mar 26 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.