The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Need help with searching for strings in sublists
Discuss Need help with searching for strings in sublists in the Python Programming forum on Dev Shed. Need help with searching for strings in sublists Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 3rd, 2012, 02:37 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 2
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 |
|
|
|
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!
|

November 3rd, 2012, 05:28 PM
|
 |
Contributing User
|
|
|
|
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!
|

November 3rd, 2012, 06:52 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 2
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!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|