check if a string contains any char from set
Actually, i'm working on a task from SPOJ. How to check if a string contains any char from set, but first char from string where a char from set occur can not be deleted.
F.e.
Have a string
word = "anAconda_elEphant"
and a set of vowels:
vowels = set('aeiouyAEIOUY')
I want in result a string
word = "ancnd_lphnt"
This should return True when occurence of any char in set is equal 1. I know that argument for a method .count() must be str, not set.
if word.count(vowels) == 1:
for char in word[char_pos:]:
if char in vowels:
char.replace('')
python
add a comment |
Actually, i'm working on a task from SPOJ. How to check if a string contains any char from set, but first char from string where a char from set occur can not be deleted.
F.e.
Have a string
word = "anAconda_elEphant"
and a set of vowels:
vowels = set('aeiouyAEIOUY')
I want in result a string
word = "ancnd_lphnt"
This should return True when occurence of any char in set is equal 1. I know that argument for a method .count() must be str, not set.
if word.count(vowels) == 1:
for char in word[char_pos:]:
if char in vowels:
char.replace('')
python
Do you want to return aTrue/False
result, or return the modified string?
– Barmar
Nov 21 '18 at 23:33
1
When you say the first char shouldn't be deleted, do you mean the first vowel shouldn't be deleted, or the first char of any type? E.g. what should be the result forword = "bace"
,bc
orbac
?
– Barmar
Nov 21 '18 at 23:35
if itsbac
that makes my solution invalid
– Joran Beasley
Nov 21 '18 at 23:36
@Barmar want a return an int which tell me how many vowels are in string. True = 1, False !=1
– Radosław Hryniewicki
Nov 22 '18 at 8:32
But you also said you want to delete vowels from the string. Do you want two returns, the count of vowels and the new string?
– Barmar
Nov 23 '18 at 6:18
add a comment |
Actually, i'm working on a task from SPOJ. How to check if a string contains any char from set, but first char from string where a char from set occur can not be deleted.
F.e.
Have a string
word = "anAconda_elEphant"
and a set of vowels:
vowels = set('aeiouyAEIOUY')
I want in result a string
word = "ancnd_lphnt"
This should return True when occurence of any char in set is equal 1. I know that argument for a method .count() must be str, not set.
if word.count(vowels) == 1:
for char in word[char_pos:]:
if char in vowels:
char.replace('')
python
Actually, i'm working on a task from SPOJ. How to check if a string contains any char from set, but first char from string where a char from set occur can not be deleted.
F.e.
Have a string
word = "anAconda_elEphant"
and a set of vowels:
vowels = set('aeiouyAEIOUY')
I want in result a string
word = "ancnd_lphnt"
This should return True when occurence of any char in set is equal 1. I know that argument for a method .count() must be str, not set.
if word.count(vowels) == 1:
for char in word[char_pos:]:
if char in vowels:
char.replace('')
python
python
edited Nov 21 '18 at 23:29
ewwink
11.6k22238
11.6k22238
asked Nov 21 '18 at 23:25
Radosław HryniewickiRadosław Hryniewicki
92
92
Do you want to return aTrue/False
result, or return the modified string?
– Barmar
Nov 21 '18 at 23:33
1
When you say the first char shouldn't be deleted, do you mean the first vowel shouldn't be deleted, or the first char of any type? E.g. what should be the result forword = "bace"
,bc
orbac
?
– Barmar
Nov 21 '18 at 23:35
if itsbac
that makes my solution invalid
– Joran Beasley
Nov 21 '18 at 23:36
@Barmar want a return an int which tell me how many vowels are in string. True = 1, False !=1
– Radosław Hryniewicki
Nov 22 '18 at 8:32
But you also said you want to delete vowels from the string. Do you want two returns, the count of vowels and the new string?
– Barmar
Nov 23 '18 at 6:18
add a comment |
Do you want to return aTrue/False
result, or return the modified string?
– Barmar
Nov 21 '18 at 23:33
1
When you say the first char shouldn't be deleted, do you mean the first vowel shouldn't be deleted, or the first char of any type? E.g. what should be the result forword = "bace"
,bc
orbac
?
– Barmar
Nov 21 '18 at 23:35
if itsbac
that makes my solution invalid
– Joran Beasley
Nov 21 '18 at 23:36
@Barmar want a return an int which tell me how many vowels are in string. True = 1, False !=1
– Radosław Hryniewicki
Nov 22 '18 at 8:32
But you also said you want to delete vowels from the string. Do you want two returns, the count of vowels and the new string?
– Barmar
Nov 23 '18 at 6:18
Do you want to return a
True/False
result, or return the modified string?– Barmar
Nov 21 '18 at 23:33
Do you want to return a
True/False
result, or return the modified string?– Barmar
Nov 21 '18 at 23:33
1
1
When you say the first char shouldn't be deleted, do you mean the first vowel shouldn't be deleted, or the first char of any type? E.g. what should be the result for
word = "bace"
, bc
or bac
?– Barmar
Nov 21 '18 at 23:35
When you say the first char shouldn't be deleted, do you mean the first vowel shouldn't be deleted, or the first char of any type? E.g. what should be the result for
word = "bace"
, bc
or bac
?– Barmar
Nov 21 '18 at 23:35
if its
bac
that makes my solution invalid– Joran Beasley
Nov 21 '18 at 23:36
if its
bac
that makes my solution invalid– Joran Beasley
Nov 21 '18 at 23:36
@Barmar want a return an int which tell me how many vowels are in string. True = 1, False !=1
– Radosław Hryniewicki
Nov 22 '18 at 8:32
@Barmar want a return an int which tell me how many vowels are in string. True = 1, False !=1
– Radosław Hryniewicki
Nov 22 '18 at 8:32
But you also said you want to delete vowels from the string. Do you want two returns, the count of vowels and the new string?
– Barmar
Nov 23 '18 at 6:18
But you also said you want to delete vowels from the string. Do you want two returns, the count of vowels and the new string?
– Barmar
Nov 23 '18 at 6:18
add a comment |
3 Answers
3
active
oldest
votes
just use a regular expression
import re
word = "anAconda_elEphant"
# use a "lookbehind" to make sure there is at least one character in front of this character...
print(re.sub("(?<=.)[aeiouyAEIOUY]",'',word))
# 'ancnd_lphnt'
as mentioned if you expect it to skip the first match of the set as opposed to just the first letter you will need a different solution
print(re.sub("(?<=.)[aeiouyAEIOUY]",'',"bace"))
# 'bc' # a is not the FIRST letter so it is replaced
the easiest is to split it into two steps
first split the string on the first match
word = "bace"
splitted_string = re.split("(.*?[aeiouyAEIOUY])",word,1)
# you will notice we have an extra empty string at the beginning of our matches ... so we can skip that
lhs,rhs = splitted_string[1:]
# now just run a simple re.sub on our rhs and rejoin the halves
print(lhs + re.sub("[aeiouyAEIOUY]",'',rhs))
# results in "bac"
Wished I had the mental capacity to finangle with my own REGEX. Nice answer
– Jaba
Nov 21 '18 at 23:42
add a comment |
You can use a for
loop as below. The idea is to build a list, and use a flag to mark when you meet a character from vowels
.
word = "anAconda_elEphant"
vowels = set('aeiouyAEIOUY')
flag = False
L =
for ch in word:
if (ch not in vowels) or (not flag):
L.append(ch)
if ch in vowels:
flag = True
word = ''.join(L)
print(word)
ancnd_lphnt
add a comment |
print(vowels.intersection(word))
https://docs.python.org/3/library/stdtypes.html#frozenset.intersection
He wants to remove the vowels, so it should beword.difference(vowels)
– Barmar
Nov 23 '18 at 6:20
actually, neither would work if there are duplicate consonants - only one of the duplicates would survive.
– rikAtee
Nov 23 '18 at 14:43
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%2f53421855%2fcheck-if-a-string-contains-any-char-from-set%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
just use a regular expression
import re
word = "anAconda_elEphant"
# use a "lookbehind" to make sure there is at least one character in front of this character...
print(re.sub("(?<=.)[aeiouyAEIOUY]",'',word))
# 'ancnd_lphnt'
as mentioned if you expect it to skip the first match of the set as opposed to just the first letter you will need a different solution
print(re.sub("(?<=.)[aeiouyAEIOUY]",'',"bace"))
# 'bc' # a is not the FIRST letter so it is replaced
the easiest is to split it into two steps
first split the string on the first match
word = "bace"
splitted_string = re.split("(.*?[aeiouyAEIOUY])",word,1)
# you will notice we have an extra empty string at the beginning of our matches ... so we can skip that
lhs,rhs = splitted_string[1:]
# now just run a simple re.sub on our rhs and rejoin the halves
print(lhs + re.sub("[aeiouyAEIOUY]",'',rhs))
# results in "bac"
Wished I had the mental capacity to finangle with my own REGEX. Nice answer
– Jaba
Nov 21 '18 at 23:42
add a comment |
just use a regular expression
import re
word = "anAconda_elEphant"
# use a "lookbehind" to make sure there is at least one character in front of this character...
print(re.sub("(?<=.)[aeiouyAEIOUY]",'',word))
# 'ancnd_lphnt'
as mentioned if you expect it to skip the first match of the set as opposed to just the first letter you will need a different solution
print(re.sub("(?<=.)[aeiouyAEIOUY]",'',"bace"))
# 'bc' # a is not the FIRST letter so it is replaced
the easiest is to split it into two steps
first split the string on the first match
word = "bace"
splitted_string = re.split("(.*?[aeiouyAEIOUY])",word,1)
# you will notice we have an extra empty string at the beginning of our matches ... so we can skip that
lhs,rhs = splitted_string[1:]
# now just run a simple re.sub on our rhs and rejoin the halves
print(lhs + re.sub("[aeiouyAEIOUY]",'',rhs))
# results in "bac"
Wished I had the mental capacity to finangle with my own REGEX. Nice answer
– Jaba
Nov 21 '18 at 23:42
add a comment |
just use a regular expression
import re
word = "anAconda_elEphant"
# use a "lookbehind" to make sure there is at least one character in front of this character...
print(re.sub("(?<=.)[aeiouyAEIOUY]",'',word))
# 'ancnd_lphnt'
as mentioned if you expect it to skip the first match of the set as opposed to just the first letter you will need a different solution
print(re.sub("(?<=.)[aeiouyAEIOUY]",'',"bace"))
# 'bc' # a is not the FIRST letter so it is replaced
the easiest is to split it into two steps
first split the string on the first match
word = "bace"
splitted_string = re.split("(.*?[aeiouyAEIOUY])",word,1)
# you will notice we have an extra empty string at the beginning of our matches ... so we can skip that
lhs,rhs = splitted_string[1:]
# now just run a simple re.sub on our rhs and rejoin the halves
print(lhs + re.sub("[aeiouyAEIOUY]",'',rhs))
# results in "bac"
just use a regular expression
import re
word = "anAconda_elEphant"
# use a "lookbehind" to make sure there is at least one character in front of this character...
print(re.sub("(?<=.)[aeiouyAEIOUY]",'',word))
# 'ancnd_lphnt'
as mentioned if you expect it to skip the first match of the set as opposed to just the first letter you will need a different solution
print(re.sub("(?<=.)[aeiouyAEIOUY]",'',"bace"))
# 'bc' # a is not the FIRST letter so it is replaced
the easiest is to split it into two steps
first split the string on the first match
word = "bace"
splitted_string = re.split("(.*?[aeiouyAEIOUY])",word,1)
# you will notice we have an extra empty string at the beginning of our matches ... so we can skip that
lhs,rhs = splitted_string[1:]
# now just run a simple re.sub on our rhs and rejoin the halves
print(lhs + re.sub("[aeiouyAEIOUY]",'',rhs))
# results in "bac"
edited Nov 21 '18 at 23:44
answered Nov 21 '18 at 23:33
Joran BeasleyJoran Beasley
72.7k678118
72.7k678118
Wished I had the mental capacity to finangle with my own REGEX. Nice answer
– Jaba
Nov 21 '18 at 23:42
add a comment |
Wished I had the mental capacity to finangle with my own REGEX. Nice answer
– Jaba
Nov 21 '18 at 23:42
Wished I had the mental capacity to finangle with my own REGEX. Nice answer
– Jaba
Nov 21 '18 at 23:42
Wished I had the mental capacity to finangle with my own REGEX. Nice answer
– Jaba
Nov 21 '18 at 23:42
add a comment |
You can use a for
loop as below. The idea is to build a list, and use a flag to mark when you meet a character from vowels
.
word = "anAconda_elEphant"
vowels = set('aeiouyAEIOUY')
flag = False
L =
for ch in word:
if (ch not in vowels) or (not flag):
L.append(ch)
if ch in vowels:
flag = True
word = ''.join(L)
print(word)
ancnd_lphnt
add a comment |
You can use a for
loop as below. The idea is to build a list, and use a flag to mark when you meet a character from vowels
.
word = "anAconda_elEphant"
vowels = set('aeiouyAEIOUY')
flag = False
L =
for ch in word:
if (ch not in vowels) or (not flag):
L.append(ch)
if ch in vowels:
flag = True
word = ''.join(L)
print(word)
ancnd_lphnt
add a comment |
You can use a for
loop as below. The idea is to build a list, and use a flag to mark when you meet a character from vowels
.
word = "anAconda_elEphant"
vowels = set('aeiouyAEIOUY')
flag = False
L =
for ch in word:
if (ch not in vowels) or (not flag):
L.append(ch)
if ch in vowels:
flag = True
word = ''.join(L)
print(word)
ancnd_lphnt
You can use a for
loop as below. The idea is to build a list, and use a flag to mark when you meet a character from vowels
.
word = "anAconda_elEphant"
vowels = set('aeiouyAEIOUY')
flag = False
L =
for ch in word:
if (ch not in vowels) or (not flag):
L.append(ch)
if ch in vowels:
flag = True
word = ''.join(L)
print(word)
ancnd_lphnt
edited Nov 21 '18 at 23:41
answered Nov 21 '18 at 23:31
jppjpp
96.7k2158109
96.7k2158109
add a comment |
add a comment |
print(vowels.intersection(word))
https://docs.python.org/3/library/stdtypes.html#frozenset.intersection
He wants to remove the vowels, so it should beword.difference(vowels)
– Barmar
Nov 23 '18 at 6:20
actually, neither would work if there are duplicate consonants - only one of the duplicates would survive.
– rikAtee
Nov 23 '18 at 14:43
add a comment |
print(vowels.intersection(word))
https://docs.python.org/3/library/stdtypes.html#frozenset.intersection
He wants to remove the vowels, so it should beword.difference(vowels)
– Barmar
Nov 23 '18 at 6:20
actually, neither would work if there are duplicate consonants - only one of the duplicates would survive.
– rikAtee
Nov 23 '18 at 14:43
add a comment |
print(vowels.intersection(word))
https://docs.python.org/3/library/stdtypes.html#frozenset.intersection
print(vowels.intersection(word))
https://docs.python.org/3/library/stdtypes.html#frozenset.intersection
answered Nov 22 '18 at 0:28
rikAteerikAtee
4,83552958
4,83552958
He wants to remove the vowels, so it should beword.difference(vowels)
– Barmar
Nov 23 '18 at 6:20
actually, neither would work if there are duplicate consonants - only one of the duplicates would survive.
– rikAtee
Nov 23 '18 at 14:43
add a comment |
He wants to remove the vowels, so it should beword.difference(vowels)
– Barmar
Nov 23 '18 at 6:20
actually, neither would work if there are duplicate consonants - only one of the duplicates would survive.
– rikAtee
Nov 23 '18 at 14:43
He wants to remove the vowels, so it should be
word.difference(vowels)
– Barmar
Nov 23 '18 at 6:20
He wants to remove the vowels, so it should be
word.difference(vowels)
– Barmar
Nov 23 '18 at 6:20
actually, neither would work if there are duplicate consonants - only one of the duplicates would survive.
– rikAtee
Nov 23 '18 at 14:43
actually, neither would work if there are duplicate consonants - only one of the duplicates would survive.
– rikAtee
Nov 23 '18 at 14:43
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.
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%2f53421855%2fcheck-if-a-string-contains-any-char-from-set%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
Do you want to return a
True/False
result, or return the modified string?– Barmar
Nov 21 '18 at 23:33
1
When you say the first char shouldn't be deleted, do you mean the first vowel shouldn't be deleted, or the first char of any type? E.g. what should be the result for
word = "bace"
,bc
orbac
?– Barmar
Nov 21 '18 at 23:35
if its
bac
that makes my solution invalid– Joran Beasley
Nov 21 '18 at 23:36
@Barmar want a return an int which tell me how many vowels are in string. True = 1, False !=1
– Radosław Hryniewicki
Nov 22 '18 at 8:32
But you also said you want to delete vowels from the string. Do you want two returns, the count of vowels and the new string?
– Barmar
Nov 23 '18 at 6:18