469,358 Members | 1,633 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

Help withselecting and returning lists

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
3 1075
2,851 Expert Mod 2GB
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']]
  4. def synonyms(search_word):
  5.     for item in thesaurus:
  6.         if search_word in item:
  7.             return item
>>> 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
849 Expert 512MB
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
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.

Similar topics

3 posts views Thread by waters | last post: by
10 posts views Thread by Michael McCracken | last post: by
10 posts views Thread by Henk van Lingen | last post: by
11 posts views Thread by JJLaRocque | last post: by
9 posts views Thread by Hank Stalica | last post: by
1 post views Thread by vikjohn | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.