Help withselecting and returning lists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Qbert16
    New Member
    • Mar 2008
    • 5

    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)
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    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. [code=Python]thesaurus = [['item1', 'item2', 'item3', 'item4'], \
    ['itema', 'itemb', 'itemc', 'itemd']]

    def synonyms(search _word):
    for item in thesaurus:
    if search_word in item:
    return item
    [/code]

    >>> 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.[code=Python]
    >>>
    >>> dd = {}
    >>> for item in thesaurus:
    ... for word in item:
    ... dd[word]=item
    ...
    >>> dd.get('item1', None)
    ['item1', 'item2', 'item3', 'item4']
    >>> print dd.get('some_wo rd', None)
    None
    >>> for key in dd:
    ... print '%s = %s' % (key, dd[key])
    ...
    item2 = ['item1', 'item2', 'item3', 'item4']
    item3 = ['item1', 'item2', 'item3', 'item4']
    item1 = ['item1', 'item2', 'item3', 'item4']
    item4 = ['item1', 'item2', 'item3', 'item4']
    itemb = ['itema', 'itemb', 'itemc', 'itemd']
    itemc = ['itema', 'itemb', 'itemc', 'itemd']
    itema = ['itema', 'itemb', 'itemc', 'itemd']
    itemd = ['itema', 'itemb', 'itemc', 'itemd']
    >>>[/code]I am sure there is a better way. HTH, bv

    Comment

    • Laharl
      Recognized Expert Contributor
      • Sep 2007
      • 849

      #3
      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.

      Comment

      • Qbert16
        New Member
        • Mar 2008
        • 5

        #4
        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.

        Comment

        Working...