compare specific string to a word python












0














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 :)










share|improve this question


















  • 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
















0














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 :)










share|improve this question


















  • 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














0












0








0







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 :)










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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














  • 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












1 Answer
1






active

oldest

votes


















0














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']





share|improve this answer





















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









    0














    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']





    share|improve this answer


























      0














      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']





      share|improve this answer
























        0












        0








        0






        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']





        share|improve this answer












        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']






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 7:25









        Edgar R. Mondragón

        1,4341619




        1,4341619






























            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.





            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.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini