compare specific string to a word python
say I have a certain string and a list of strings.
I would like to append to a new list all the words from the list (of strings)
that are exactly like the pattern
for example:
list of strings = ['string1','string2'...]
pattern =__letter__letter_ ('_c__ye_' for instance)
I need to add all strings that are made up of the same letters in the same places as the pattern, and has the same length.
so for instance:
new_list = ['aczxyep','zcisyef'...]
I have tried this:
def pattern_word_equality(words,pattern):
list1 =
for word in words:
for letter in word:
if letter in pattern:
list1.append(word)
return list1
help will be much appreciated :)
python string list word
|
show 3 more comments
say I have a certain string and a list of strings.
I would like to append to a new list all the words from the list (of strings)
that are exactly like the pattern
for example:
list of strings = ['string1','string2'...]
pattern =__letter__letter_ ('_c__ye_' for instance)
I need to add all strings that are made up of the same letters in the same places as the pattern, and has the same length.
so for instance:
new_list = ['aczxyep','zcisyef'...]
I have tried this:
def pattern_word_equality(words,pattern):
list1 =
for word in words:
for letter in word:
if letter in pattern:
list1.append(word)
return list1
help will be much appreciated :)
python string list word
2
more hangman homework? :) Someone asked this yesterday too funnily enough.
– Paritosh Singh
Nov 11 at 17:01
haha it was me but the question wasnt specific enough anyway..
– user10596917
Nov 11 at 17:01
oh! did the solution not work for you?
– Paritosh Singh
Nov 11 at 17:02
Noo.. I mean the nice people tried to help but I gave an example that made it seem like the situation isnt exactly what it was. (I gave an example for a pattern that STARTS with letters, though it is actually a random pattern)
– user10596917
Nov 11 at 17:04
stackoverflow.com/a/53242554/10618540 posted there will work the same, doesnt matter if the string didnt start with it.
– Paritosh Singh
Nov 11 at 17:05
|
show 3 more comments
say I have a certain string and a list of strings.
I would like to append to a new list all the words from the list (of strings)
that are exactly like the pattern
for example:
list of strings = ['string1','string2'...]
pattern =__letter__letter_ ('_c__ye_' for instance)
I need to add all strings that are made up of the same letters in the same places as the pattern, and has the same length.
so for instance:
new_list = ['aczxyep','zcisyef'...]
I have tried this:
def pattern_word_equality(words,pattern):
list1 =
for word in words:
for letter in word:
if letter in pattern:
list1.append(word)
return list1
help will be much appreciated :)
python string list word
say I have a certain string and a list of strings.
I would like to append to a new list all the words from the list (of strings)
that are exactly like the pattern
for example:
list of strings = ['string1','string2'...]
pattern =__letter__letter_ ('_c__ye_' for instance)
I need to add all strings that are made up of the same letters in the same places as the pattern, and has the same length.
so for instance:
new_list = ['aczxyep','zcisyef'...]
I have tried this:
def pattern_word_equality(words,pattern):
list1 =
for word in words:
for letter in word:
if letter in pattern:
list1.append(word)
return list1
help will be much appreciated :)
python string list word
python string list word
asked Nov 11 at 16:53
user10596917
2
more hangman homework? :) Someone asked this yesterday too funnily enough.
– Paritosh Singh
Nov 11 at 17:01
haha it was me but the question wasnt specific enough anyway..
– user10596917
Nov 11 at 17:01
oh! did the solution not work for you?
– Paritosh Singh
Nov 11 at 17:02
Noo.. I mean the nice people tried to help but I gave an example that made it seem like the situation isnt exactly what it was. (I gave an example for a pattern that STARTS with letters, though it is actually a random pattern)
– user10596917
Nov 11 at 17:04
stackoverflow.com/a/53242554/10618540 posted there will work the same, doesnt matter if the string didnt start with it.
– Paritosh Singh
Nov 11 at 17:05
|
show 3 more comments
2
more hangman homework? :) Someone asked this yesterday too funnily enough.
– Paritosh Singh
Nov 11 at 17:01
haha it was me but the question wasnt specific enough anyway..
– user10596917
Nov 11 at 17:01
oh! did the solution not work for you?
– Paritosh Singh
Nov 11 at 17:02
Noo.. I mean the nice people tried to help but I gave an example that made it seem like the situation isnt exactly what it was. (I gave an example for a pattern that STARTS with letters, though it is actually a random pattern)
– user10596917
Nov 11 at 17:04
stackoverflow.com/a/53242554/10618540 posted there will work the same, doesnt matter if the string didnt start with it.
– Paritosh Singh
Nov 11 at 17:05
2
2
more hangman homework? :) Someone asked this yesterday too funnily enough.
– Paritosh Singh
Nov 11 at 17:01
more hangman homework? :) Someone asked this yesterday too funnily enough.
– Paritosh Singh
Nov 11 at 17:01
haha it was me but the question wasnt specific enough anyway..
– user10596917
Nov 11 at 17:01
haha it was me but the question wasnt specific enough anyway..
– user10596917
Nov 11 at 17:01
oh! did the solution not work for you?
– Paritosh Singh
Nov 11 at 17:02
oh! did the solution not work for you?
– Paritosh Singh
Nov 11 at 17:02
Noo.. I mean the nice people tried to help but I gave an example that made it seem like the situation isnt exactly what it was. (I gave an example for a pattern that STARTS with letters, though it is actually a random pattern)
– user10596917
Nov 11 at 17:04
Noo.. I mean the nice people tried to help but I gave an example that made it seem like the situation isnt exactly what it was. (I gave an example for a pattern that STARTS with letters, though it is actually a random pattern)
– user10596917
Nov 11 at 17:04
stackoverflow.com/a/53242554/10618540 posted there will work the same, doesnt matter if the string didnt start with it.
– Paritosh Singh
Nov 11 at 17:05
stackoverflow.com/a/53242554/10618540 posted there will work the same, doesnt matter if the string didnt start with it.
– Paritosh Singh
Nov 11 at 17:05
|
show 3 more comments
1 Answer
1
active
oldest
votes
If your pattern is as simple as _c__ye_, then you can look for the characters in the specific positions:
words = ['aczxyep', 'cxxye', 'zcisyef', 'abcdefg']
result1 = list(filter(lambda w: w[1] == 'c' and w[4:6] == 'ye', words))
If your pattern is getting more complex, then you can start using regular expressions:
pat = re.compile("^.c..ye.$")
result2 = list(filter(lambda w: pat.match(w), words))
Output:
print(result1) # ['aczxyep', 'zcisyef']
print(result2) # ['aczxyep', 'zcisyef']
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53251010%2fcompare-specific-string-to-a-word-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If your pattern is as simple as _c__ye_, then you can look for the characters in the specific positions:
words = ['aczxyep', 'cxxye', 'zcisyef', 'abcdefg']
result1 = list(filter(lambda w: w[1] == 'c' and w[4:6] == 'ye', words))
If your pattern is getting more complex, then you can start using regular expressions:
pat = re.compile("^.c..ye.$")
result2 = list(filter(lambda w: pat.match(w), words))
Output:
print(result1) # ['aczxyep', 'zcisyef']
print(result2) # ['aczxyep', 'zcisyef']
add a comment |
If your pattern is as simple as _c__ye_, then you can look for the characters in the specific positions:
words = ['aczxyep', 'cxxye', 'zcisyef', 'abcdefg']
result1 = list(filter(lambda w: w[1] == 'c' and w[4:6] == 'ye', words))
If your pattern is getting more complex, then you can start using regular expressions:
pat = re.compile("^.c..ye.$")
result2 = list(filter(lambda w: pat.match(w), words))
Output:
print(result1) # ['aczxyep', 'zcisyef']
print(result2) # ['aczxyep', 'zcisyef']
add a comment |
If your pattern is as simple as _c__ye_, then you can look for the characters in the specific positions:
words = ['aczxyep', 'cxxye', 'zcisyef', 'abcdefg']
result1 = list(filter(lambda w: w[1] == 'c' and w[4:6] == 'ye', words))
If your pattern is getting more complex, then you can start using regular expressions:
pat = re.compile("^.c..ye.$")
result2 = list(filter(lambda w: pat.match(w), words))
Output:
print(result1) # ['aczxyep', 'zcisyef']
print(result2) # ['aczxyep', 'zcisyef']
If your pattern is as simple as _c__ye_, then you can look for the characters in the specific positions:
words = ['aczxyep', 'cxxye', 'zcisyef', 'abcdefg']
result1 = list(filter(lambda w: w[1] == 'c' and w[4:6] == 'ye', words))
If your pattern is getting more complex, then you can start using regular expressions:
pat = re.compile("^.c..ye.$")
result2 = list(filter(lambda w: pat.match(w), words))
Output:
print(result1) # ['aczxyep', 'zcisyef']
print(result2) # ['aczxyep', 'zcisyef']
answered Nov 12 at 7:25
Edgar R. Mondragón
1,4341619
1,4341619
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53251010%2fcompare-specific-string-to-a-word-python%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
2
more hangman homework? :) Someone asked this yesterday too funnily enough.
– Paritosh Singh
Nov 11 at 17:01
haha it was me but the question wasnt specific enough anyway..
– user10596917
Nov 11 at 17:01
oh! did the solution not work for you?
– Paritosh Singh
Nov 11 at 17:02
Noo.. I mean the nice people tried to help but I gave an example that made it seem like the situation isnt exactly what it was. (I gave an example for a pattern that STARTS with letters, though it is actually a random pattern)
– user10596917
Nov 11 at 17:04
stackoverflow.com/a/53242554/10618540 posted there will work the same, doesnt matter if the string didnt start with it.
– Paritosh Singh
Nov 11 at 17:05