compare items from different lists
up vote
0
down vote
favorite
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!
python list
add a comment |
up vote
0
down vote
favorite
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!
python list
what are you using for input? looks like the top line that is returned only has 1 error which is first aaron. Other match the ABA sequence
– I am Cavic
Nov 10 at 19:14
@Austin only words with the same length it seems
– Jon Clements♦
Nov 10 at 19:16
@Austin yeah... one or the other doesn't seem right - code with thelen
check implies it shouldn't be there though.... :)
– Jon Clements♦
Nov 10 at 19:18
hey sry had a slight error there, edited it. anyway - aback and abaft shows, but two times. and also I get aaron (which shouldn't be there)
– python_beginner
Nov 10 at 19:30
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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!
python list
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!
python list
python list
edited Nov 10 at 19:46
asked Nov 10 at 19:07
python_beginner
165
165
what are you using for input? looks like the top line that is returned only has 1 error which is first aaron. Other match the ABA sequence
– I am Cavic
Nov 10 at 19:14
@Austin only words with the same length it seems
– Jon Clements♦
Nov 10 at 19:16
@Austin yeah... one or the other doesn't seem right - code with thelen
check implies it shouldn't be there though.... :)
– Jon Clements♦
Nov 10 at 19:18
hey sry had a slight error there, edited it. anyway - aback and abaft shows, but two times. and also I get aaron (which shouldn't be there)
– python_beginner
Nov 10 at 19:30
add a comment |
what are you using for input? looks like the top line that is returned only has 1 error which is first aaron. Other match the ABA sequence
– I am Cavic
Nov 10 at 19:14
@Austin only words with the same length it seems
– Jon Clements♦
Nov 10 at 19:16
@Austin yeah... one or the other doesn't seem right - code with thelen
check implies it shouldn't be there though.... :)
– Jon Clements♦
Nov 10 at 19:18
hey sry had a slight error there, edited it. anyway - aback and abaft shows, but two times. and also I get aaron (which shouldn't be there)
– python_beginner
Nov 10 at 19:30
what are you using for input? looks like the top line that is returned only has 1 error which is first aaron. Other match the ABA sequence
– I am Cavic
Nov 10 at 19:14
what are you using for input? looks like the top line that is returned only has 1 error which is first aaron. Other match the ABA sequence
– I am Cavic
Nov 10 at 19:14
@Austin only words with the same length it seems
– Jon Clements♦
Nov 10 at 19:16
@Austin only words with the same length it seems
– Jon Clements♦
Nov 10 at 19:16
@Austin yeah... one or the other doesn't seem right - code with the
len
check implies it shouldn't be there though.... :)– Jon Clements♦
Nov 10 at 19:18
@Austin yeah... one or the other doesn't seem right - code with the
len
check implies it shouldn't be there though.... :)– Jon Clements♦
Nov 10 at 19:18
hey sry had a slight error there, edited it. anyway - aback and abaft shows, but two times. and also I get aaron (which shouldn't be there)
– python_beginner
Nov 10 at 19:30
hey sry had a slight error there, edited it. anyway - aback and abaft shows, but two times. and also I get aaron (which shouldn't be there)
– python_beginner
Nov 10 at 19:30
add a comment |
4 Answers
4
active
oldest
votes
up vote
1
down vote
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.
Beautiful. The key insight for the OP would be to recognize that strings in python are iterables, and can be used in zips as a consequence.
– Paritosh Singh
Nov 10 at 19:20
add a comment |
up vote
1
down vote
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...
...that would have been my go-to as well!
– hiro protagonist
Nov 10 at 19:23
add a comment |
up vote
0
down vote
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.
can you explain the startswitch part and pat? thanks
– python_beginner
Nov 10 at 19:33
Basically, what I am doing is fetching ab from the pattern "ab___". Now we have a method in python called as "startswith" which check if a string starts with the particular string. So in this case it checks if a word starts with "ab". Check out the startswith documentation : programiz.com/python-programming/methods/string/startswith
– Sanchit Kumar
Nov 10 at 19:39
oh so it wouldnt work for a pattern that the string isnt at the start?
– python_beginner
Nov 10 at 19:41
add a comment |
up vote
0
down vote
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']
can you explain the split part and the startswitch ?
– python_beginner
Nov 10 at 19:33
I used split here to remove part with "_" characters and have pure pattern, to later check if word starts with it.
– Filip Młynarski
Nov 11 at 9:33
what if I want not only that it will start with, but will have it and only it ? for example, the words: catch fetch hatch, tache the pattern is _tc so the output will be catch fetch and hatch (withuot tache)
– python_beginner
Nov 11 at 13:02
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
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.
Beautiful. The key insight for the OP would be to recognize that strings in python are iterables, and can be used in zips as a consequence.
– Paritosh Singh
Nov 10 at 19:20
add a comment |
up vote
1
down vote
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.
Beautiful. The key insight for the OP would be to recognize that strings in python are iterables, and can be used in zips as a consequence.
– Paritosh Singh
Nov 10 at 19:20
add a comment |
up vote
1
down vote
up vote
1
down vote
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.
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.
edited Nov 10 at 19:20
answered Nov 10 at 19:18
hiro protagonist
17.5k63660
17.5k63660
Beautiful. The key insight for the OP would be to recognize that strings in python are iterables, and can be used in zips as a consequence.
– Paritosh Singh
Nov 10 at 19:20
add a comment |
Beautiful. The key insight for the OP would be to recognize that strings in python are iterables, and can be used in zips as a consequence.
– Paritosh Singh
Nov 10 at 19:20
Beautiful. The key insight for the OP would be to recognize that strings in python are iterables, and can be used in zips as a consequence.
– Paritosh Singh
Nov 10 at 19:20
Beautiful. The key insight for the OP would be to recognize that strings in python are iterables, and can be used in zips as a consequence.
– Paritosh Singh
Nov 10 at 19:20
add a comment |
up vote
1
down vote
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...
...that would have been my go-to as well!
– hiro protagonist
Nov 10 at 19:23
add a comment |
up vote
1
down vote
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...
...that would have been my go-to as well!
– hiro protagonist
Nov 10 at 19:23
add a comment |
up vote
1
down vote
up vote
1
down vote
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...
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...
edited Nov 10 at 19:36
answered Nov 10 at 19:22
Jon Clements♦
97.1k19169214
97.1k19169214
...that would have been my go-to as well!
– hiro protagonist
Nov 10 at 19:23
add a comment |
...that would have been my go-to as well!
– hiro protagonist
Nov 10 at 19:23
...that would have been my go-to as well!
– hiro protagonist
Nov 10 at 19:23
...that would have been my go-to as well!
– hiro protagonist
Nov 10 at 19:23
add a comment |
up vote
0
down vote
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.
can you explain the startswitch part and pat? thanks
– python_beginner
Nov 10 at 19:33
Basically, what I am doing is fetching ab from the pattern "ab___". Now we have a method in python called as "startswith" which check if a string starts with the particular string. So in this case it checks if a word starts with "ab". Check out the startswith documentation : programiz.com/python-programming/methods/string/startswith
– Sanchit Kumar
Nov 10 at 19:39
oh so it wouldnt work for a pattern that the string isnt at the start?
– python_beginner
Nov 10 at 19:41
add a comment |
up vote
0
down vote
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.
can you explain the startswitch part and pat? thanks
– python_beginner
Nov 10 at 19:33
Basically, what I am doing is fetching ab from the pattern "ab___". Now we have a method in python called as "startswith" which check if a string starts with the particular string. So in this case it checks if a word starts with "ab". Check out the startswith documentation : programiz.com/python-programming/methods/string/startswith
– Sanchit Kumar
Nov 10 at 19:39
oh so it wouldnt work for a pattern that the string isnt at the start?
– python_beginner
Nov 10 at 19:41
add a comment |
up vote
0
down vote
up vote
0
down vote
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.
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.
answered Nov 10 at 19:14
Sanchit Kumar
2577
2577
can you explain the startswitch part and pat? thanks
– python_beginner
Nov 10 at 19:33
Basically, what I am doing is fetching ab from the pattern "ab___". Now we have a method in python called as "startswith" which check if a string starts with the particular string. So in this case it checks if a word starts with "ab". Check out the startswith documentation : programiz.com/python-programming/methods/string/startswith
– Sanchit Kumar
Nov 10 at 19:39
oh so it wouldnt work for a pattern that the string isnt at the start?
– python_beginner
Nov 10 at 19:41
add a comment |
can you explain the startswitch part and pat? thanks
– python_beginner
Nov 10 at 19:33
Basically, what I am doing is fetching ab from the pattern "ab___". Now we have a method in python called as "startswith" which check if a string starts with the particular string. So in this case it checks if a word starts with "ab". Check out the startswith documentation : programiz.com/python-programming/methods/string/startswith
– Sanchit Kumar
Nov 10 at 19:39
oh so it wouldnt work for a pattern that the string isnt at the start?
– python_beginner
Nov 10 at 19:41
can you explain the startswitch part and pat? thanks
– python_beginner
Nov 10 at 19:33
can you explain the startswitch part and pat? thanks
– python_beginner
Nov 10 at 19:33
Basically, what I am doing is fetching ab from the pattern "ab___". Now we have a method in python called as "startswith" which check if a string starts with the particular string. So in this case it checks if a word starts with "ab". Check out the startswith documentation : programiz.com/python-programming/methods/string/startswith
– Sanchit Kumar
Nov 10 at 19:39
Basically, what I am doing is fetching ab from the pattern "ab___". Now we have a method in python called as "startswith" which check if a string starts with the particular string. So in this case it checks if a word starts with "ab". Check out the startswith documentation : programiz.com/python-programming/methods/string/startswith
– Sanchit Kumar
Nov 10 at 19:39
oh so it wouldnt work for a pattern that the string isnt at the start?
– python_beginner
Nov 10 at 19:41
oh so it wouldnt work for a pattern that the string isnt at the start?
– python_beginner
Nov 10 at 19:41
add a comment |
up vote
0
down vote
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']
can you explain the split part and the startswitch ?
– python_beginner
Nov 10 at 19:33
I used split here to remove part with "_" characters and have pure pattern, to later check if word starts with it.
– Filip Młynarski
Nov 11 at 9:33
what if I want not only that it will start with, but will have it and only it ? for example, the words: catch fetch hatch, tache the pattern is _tc so the output will be catch fetch and hatch (withuot tache)
– python_beginner
Nov 11 at 13:02
add a comment |
up vote
0
down vote
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']
can you explain the split part and the startswitch ?
– python_beginner
Nov 10 at 19:33
I used split here to remove part with "_" characters and have pure pattern, to later check if word starts with it.
– Filip Młynarski
Nov 11 at 9:33
what if I want not only that it will start with, but will have it and only it ? for example, the words: catch fetch hatch, tache the pattern is _tc so the output will be catch fetch and hatch (withuot tache)
– python_beginner
Nov 11 at 13:02
add a comment |
up vote
0
down vote
up vote
0
down vote
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']
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']
answered Nov 10 at 19:19
Filip Młynarski
40717
40717
can you explain the split part and the startswitch ?
– python_beginner
Nov 10 at 19:33
I used split here to remove part with "_" characters and have pure pattern, to later check if word starts with it.
– Filip Młynarski
Nov 11 at 9:33
what if I want not only that it will start with, but will have it and only it ? for example, the words: catch fetch hatch, tache the pattern is _tc so the output will be catch fetch and hatch (withuot tache)
– python_beginner
Nov 11 at 13:02
add a comment |
can you explain the split part and the startswitch ?
– python_beginner
Nov 10 at 19:33
I used split here to remove part with "_" characters and have pure pattern, to later check if word starts with it.
– Filip Młynarski
Nov 11 at 9:33
what if I want not only that it will start with, but will have it and only it ? for example, the words: catch fetch hatch, tache the pattern is _tc so the output will be catch fetch and hatch (withuot tache)
– python_beginner
Nov 11 at 13:02
can you explain the split part and the startswitch ?
– python_beginner
Nov 10 at 19:33
can you explain the split part and the startswitch ?
– python_beginner
Nov 10 at 19:33
I used split here to remove part with "_" characters and have pure pattern, to later check if word starts with it.
– Filip Młynarski
Nov 11 at 9:33
I used split here to remove part with "_" characters and have pure pattern, to later check if word starts with it.
– Filip Młynarski
Nov 11 at 9:33
what if I want not only that it will start with, but will have it and only it ? for example, the words: catch fetch hatch, tache the pattern is _tc so the output will be catch fetch and hatch (withuot tache)
– python_beginner
Nov 11 at 13:02
what if I want not only that it will start with, but will have it and only it ? for example, the words: catch fetch hatch, tache the pattern is _tc so the output will be catch fetch and hatch (withuot tache)
– python_beginner
Nov 11 at 13:02
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242460%2fcompare-items-from-different-lists%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what are you using for input? looks like the top line that is returned only has 1 error which is first aaron. Other match the ABA sequence
– I am Cavic
Nov 10 at 19:14
@Austin only words with the same length it seems
– Jon Clements♦
Nov 10 at 19:16
@Austin yeah... one or the other doesn't seem right - code with the
len
check implies it shouldn't be there though.... :)– Jon Clements♦
Nov 10 at 19:18
hey sry had a slight error there, edited it. anyway - aback and abaft shows, but two times. and also I get aaron (which shouldn't be there)
– python_beginner
Nov 10 at 19:30