Reputation:
I have a function that is a part of hangman, that gets two inputs
filter_words_list(words, pattern):
I need a way to know that the letter in a word from the words in a specific spot, is identical to the letter (that is uncovered) in the pattern and that those letters are IN THE SAME PLACE. also, word and pattern ought to be in the same length
this is what ive tried:
def filter_words_list(words, pattern):
relevant_words = []
for word in words:
if len(word) == len(pattern):
for i in range(len(word)):
for j in range(len(pattern)):
if word[i] == pattern[j] and i == j:
relevant_words.append(word)
print(relevant_words)
filter_words_list(['aardvark', 'aardwolf', 'aaron', 'aback', 'abacus',
'abaft', 'abalone'],'ab___',))
print: not good.. as you can see here:
['aaron', 'aback', 'aback', 'abaft', 'abaft']
the print I need:
['aback', 'abaft']
thanks!
Upvotes: 0
Views: 60
Reputation: 142146
If you use .
instead of _
for your missing character then you've basically got a regular expression, eg:
import re
words = ['aardvark', 'aardwolf', 'aaron', 'aback', 'abacus', 'abaft', 'abalone']
# starting with ab followed by exactly 3 characters ($ means match end of string)
wanted = [word for word in words if re.match('ab...$')]
# ['aback', 'abaft']
# starting with ab followed by at least 3 characters (notice no $ here)
wanted2 = [word for word in words if re.match('ab...', word)]
# ['aback', 'abacus', 'abaft', 'abalone']
# starting with ab, followed by any letter, followed by "f", and exactly one letter
wanted3 = [word for word in words if re.match('ab.f.$', word)]
# ['abaft']
# etc...
Upvotes: 1
Reputation: 46859
something like this might work:
words = ('aardvark', 'aardwolf', 'aaron', 'aback', 'abacus',
'abaft', 'abalone')
pattern = 'ab___'
def match(word, pattern):
# also need to match the length?
# if len(word) != len(pattern):
# return False
for letter, p in zip(word, pattern):
if p == '_':
continue
if letter != p:
return False
return True
matches = [word for word in words if match(word, pattern)]
print(matches)
it compares one letter of the word against one character of the pattern (using zip
in order to iterate over those pairs). it ignores if the character of the pattern is '_'
.
as the function is written now it does not consider the length of either the word or the pattern.
Upvotes: 1
Reputation: 3612
This code will filter words that start with supplied pattern and are as long as that pattern (including "_" characters):
def filter_words_list(words, pattern):
pattern_len = len(pattern)
pattern = pattern.split('_')[0]
relevant_words = []
for word in words:
if len(word) == pattern_len and word.startswith(pattern):
relevant_words.append(word)
return relevant_words
words = ['aardvark', 'aardwolf', 'aaron', 'aback', 'abacus', 'abaft', 'abalone']
pattern = 'ab___'
print(filter_words_list(words, pattern)) # ['aback', 'abaft']
Upvotes: 0
Reputation: 1695
Check the below code:
def filter_words_list(words, pattern):
relevant_words = []
pat = pattern.replace("_","")
for word in words:
if word.startswith(pat):
relevant_words.append(word)
print(relevant_words)
filter_words_list(['aardvark', 'aardwolf', 'aaron', 'aback', 'abacus','abaft', 'abalone'],'ab___',)
Output:
['aback', 'abacus', 'abaft', 'abalone']
Note: Its gonna work only in the case where the pattern is given such as it starts with certain characters. For e.g, it's not gonna work in case if the pattern is "__a_". Please edit your question if you need something like that.
Upvotes: 0