check if a string contains any char from set












-1















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('')









share|improve this question

























  • 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 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











  • @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
















-1















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('')









share|improve this question

























  • 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 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











  • @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














-1












-1








-1








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('')









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 23:29









ewwink

12.2k22440




12.2k22440










asked Nov 21 '18 at 23:25









Radosław HryniewickiRadosław Hryniewicki

92




92













  • 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 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











  • @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






  • 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













  • 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

















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












3 Answers
3






active

oldest

votes


















1














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"





share|improve this answer


























  • Wished I had the mental capacity to finangle with my own REGEX. Nice answer

    – Jab
    Nov 21 '18 at 23:42



















0














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





share|improve this answer

































    0














    print(vowels.intersection(word))


    https://docs.python.org/3/library/stdtypes.html#frozenset.intersection






    share|improve this answer
























    • 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












    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    1














    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"





    share|improve this answer


























    • Wished I had the mental capacity to finangle with my own REGEX. Nice answer

      – Jab
      Nov 21 '18 at 23:42
















    1














    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"





    share|improve this answer


























    • Wished I had the mental capacity to finangle with my own REGEX. Nice answer

      – Jab
      Nov 21 '18 at 23:42














    1












    1








    1







    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"





    share|improve this answer















    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"






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 21 '18 at 23:44

























    answered Nov 21 '18 at 23:33









    Joran BeasleyJoran Beasley

    74k682121




    74k682121













    • Wished I had the mental capacity to finangle with my own REGEX. Nice answer

      – Jab
      Nov 21 '18 at 23:42



















    • Wished I had the mental capacity to finangle with my own REGEX. Nice answer

      – Jab
      Nov 21 '18 at 23:42

















    Wished I had the mental capacity to finangle with my own REGEX. Nice answer

    – Jab
    Nov 21 '18 at 23:42





    Wished I had the mental capacity to finangle with my own REGEX. Nice answer

    – Jab
    Nov 21 '18 at 23:42













    0














    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





    share|improve this answer






























      0














      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





      share|improve this answer




























        0












        0








        0







        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





        share|improve this answer















        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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 '18 at 23:41

























        answered Nov 21 '18 at 23:31









        jppjpp

        102k2165116




        102k2165116























            0














            print(vowels.intersection(word))


            https://docs.python.org/3/library/stdtypes.html#frozenset.intersection






            share|improve this answer
























            • 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
















            0














            print(vowels.intersection(word))


            https://docs.python.org/3/library/stdtypes.html#frozenset.intersection






            share|improve this answer
























            • 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














            0












            0








            0







            print(vowels.intersection(word))


            https://docs.python.org/3/library/stdtypes.html#frozenset.intersection






            share|improve this answer













            print(vowels.intersection(word))


            https://docs.python.org/3/library/stdtypes.html#frozenset.intersection







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 '18 at 0:28









            rikAteerikAtee

            4,96553059




            4,96553059













            • 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



















            • 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

















            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


















            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Can I use Tabulator js library in my java Spring + Thymeleaf project?