Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 3rd, 2012, 02:37 PM
nump_teey nump_teey is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 nump_teey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 59 sec
Reputation 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:
Original - python Code
  1. def str_from_indices(letter_list, index_number):
  2.     ''' (list of list of str, int) -> str
  3.     Return string from letter_list using index_number.
  4.     e.g.
  5.     >>> str_from_indices([['A', 'B', 'C', 'D'], ['E', 'F', 'G', 'H']], 1)
  6.     'BF'
  7.     '''
  8.  
  9.     letters_from_index = ""
  10.     for letter in letter_list:
  11.         letters_from_index = letters_from_index + str(letter[index_number])
  12.  
  13.     return letters_from_index
  14.  
  15.  
  16. def word_in_indices(letter_list, word):
  17.     ''' (list of list of str, str) -> bool
  18.     Return True if word appears in an index of letter_list.
  19.     e.g.
  20.     >>> word_in_indices([['A', 'B', 'C', 'D'], ['E', 'F', 'G', 'H']], 'NO')
  21.     False
  22.     >>> word_in_indices([['A', 'B', 'C', 'D'], ['E', 'F', 'G', 'H']], 'DH')
  23.     True
  24.     '''
  25.  
  26.     for index_number in range(len(letter_list)):
  27.         if word in str_from_indices(letter_list, index_number):
  28.             return True
  29.         else:
  30.             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!

Reply With Quote
  #2  
Old November 3rd, 2012, 05:28 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,364 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 10 h 28 m 48 sec
Reputation Power: 383
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!

Reply With Quote
  #3  
Old November 3rd, 2012, 06:52 PM
nump_teey nump_teey is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 nump_teey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 59 sec
Reputation 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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Need help with searching for strings in sublists

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap