#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Need help with searching for strings in sublists


    Hi, I'm new to Python and programming. I'm trying to check whether a string occurs in sublists. In this case the check isn't whether the string appears in one sublist, it is whether the string matches the letters at a specific index in the sublists. I have defined 2 functions for this, the 2nd function reuses the first:

    python Code:
    def str_from_indices(letter_list, index_number):
        ''' (list of list of str, int) -> str
        Return string from letter_list using index_number.
        e.g.
        >>> str_from_indices([['A', 'B', 'C', 'D'], ['E', 'F', 'G', 'H']], 1)
        'BF'
        '''
     
        letters_from_index = ""
        for letter in letter_list:
            letters_from_index = letters_from_index + str(letter[index_number])
     
        return letters_from_index
     
     
    def word_in_indices(letter_list, word):
        ''' (list of list of str, str) -> bool
        Return True if word appears in an index of letter_list.
        e.g.
        >>> word_in_indices([['A', 'B', 'C', 'D'], ['E', 'F', 'G', 'H']], 'NO')
        False
        >>> word_in_indices([['A', 'B', 'C', 'D'], ['E', 'F', 'G', 'H']], 'DH')
        True
        '''
     
        for index_number in range(len(letter_list)):
            if word in str_from_indices(letter_list, index_number):
                return True
            else:
                return False


    I think the for loop in the 2nd function and the use of range and len is where I'm going wrong, my function is only checking indices 0 & 1 so I'm getting a return of False everytime I try to find a string in indices 2 & 3.

    I'm looking for an answer ideally, although a nudge in the right direction to fixing the code would also be great!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    I fixed word_in_indices. Thank you immensely for providing doctests.

    Code:
    '''
    $ python -m doctest q.py
    ['AE', 'BF', 'CG', 'DH']
    ['AE', 'BF', 'CG', 'DH']
    '''
    
    import sys
    
    def str_from_indices(letter_list, index_number):
        ''' (list of list of str, int) -> str
        Return string from letter_list using index_number.
        e.g.
        >>> str_from_indices([['A', 'B', 'C', 'D'], ['E', 'F', 'G', 'H']], 1)
        'BF'
        '''
    
        letters_from_index = ""
        for letter in letter_list:
            letters_from_index = letters_from_index + str(letter[index_number])
    
        return letters_from_index
    
    def word_in_indices(letter_list, word):
        ''' (list of list of str, str) -> bool
        Return True if word appears in an index of letter_list.
        e.g.
        >>> word_in_indices([['A', 'B', 'C', 'D'], ['E', 'F', 'G', 'H']], 'NO')
        False
        >>> word_in_indices([['A', 'B', 'C', 'D'], ['E', 'F', 'G', 'H']], 'DH')
        True
        '''
    
        # Assume the lists in letter_list have equal length.
        # Form all the words.
        words = []
        for i in range(len(letter_list[0])):
            words.append(str_from_indices(letter_list,i))
    
        # debugging, display all the words to stderr.
        # This way doctest passes.
        sys.stderr.write(str(words)+'\n')
    
        # decide if the word appears, and return the result
        return word in words
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0
    Thanks so much for the prompt feedback. I did find a way in the end to make the function work as I wanted, but it is great to have a different take on the problem!

IMN logo majestic logo threadwatch logo seochat tools logo