Create a list of strings with one/multiple character replacement












0















How to create a whole list of string from one string where each string in the list containing exactly one character replacement? The string itself is consisted of only four characters (say: A, B, C, and D), so that the whole list of a string of length n would contain 3n+1 strings with exactly one character replacement.



Example:
inputstr = 'ABCD'
output = ['ABCD', 'BBCD', 'CBCD', 'DBCD', 'AACD', 'ACCD', 'ADCD', 'ABAD', 'ABBD', 'ABDD', 'ABCA', 'ABCB', 'ABCC']



I write the following python code:



strin = 'ABCD'
strout = set()

tempstr1 = ''
tempstr2 = ''
tempstr3 = ''
tempstr4 = ''
for base in range(len(strin)):
if strin[base] == 'A': #this block will be repeated for char B, C and D
tempstr1 = strin.replace(strin[base], 'A')
strout.add(tempstr1)
tempstr1 = ''

tempstr2 = strin.replace(strin[base], 'B')
strout.add(tempstr2)
tempstr2 = ''

tempstr3 = strin.replace(strin[base], 'C')
strout.add(tempseq3)
tempstr3 = ''

tempstr4 = strin.replace(strin[base], 'D')
strout.add(tempseq4)
tempstr4 = ''

return strout


and it works well as long as there is no repeated character (such as 'ABCD'). However, when the input string contains repeated character (such as 'AACD'), it will return less than 3n+1 string. I tried with 'AACD' string and it returns only 10 instead of 13 strings.



Anyone can help?










share|improve this question





























    0















    How to create a whole list of string from one string where each string in the list containing exactly one character replacement? The string itself is consisted of only four characters (say: A, B, C, and D), so that the whole list of a string of length n would contain 3n+1 strings with exactly one character replacement.



    Example:
    inputstr = 'ABCD'
    output = ['ABCD', 'BBCD', 'CBCD', 'DBCD', 'AACD', 'ACCD', 'ADCD', 'ABAD', 'ABBD', 'ABDD', 'ABCA', 'ABCB', 'ABCC']



    I write the following python code:



    strin = 'ABCD'
    strout = set()

    tempstr1 = ''
    tempstr2 = ''
    tempstr3 = ''
    tempstr4 = ''
    for base in range(len(strin)):
    if strin[base] == 'A': #this block will be repeated for char B, C and D
    tempstr1 = strin.replace(strin[base], 'A')
    strout.add(tempstr1)
    tempstr1 = ''

    tempstr2 = strin.replace(strin[base], 'B')
    strout.add(tempstr2)
    tempstr2 = ''

    tempstr3 = strin.replace(strin[base], 'C')
    strout.add(tempseq3)
    tempstr3 = ''

    tempstr4 = strin.replace(strin[base], 'D')
    strout.add(tempseq4)
    tempstr4 = ''

    return strout


    and it works well as long as there is no repeated character (such as 'ABCD'). However, when the input string contains repeated character (such as 'AACD'), it will return less than 3n+1 string. I tried with 'AACD' string and it returns only 10 instead of 13 strings.



    Anyone can help?










    share|improve this question



























      0












      0








      0








      How to create a whole list of string from one string where each string in the list containing exactly one character replacement? The string itself is consisted of only four characters (say: A, B, C, and D), so that the whole list of a string of length n would contain 3n+1 strings with exactly one character replacement.



      Example:
      inputstr = 'ABCD'
      output = ['ABCD', 'BBCD', 'CBCD', 'DBCD', 'AACD', 'ACCD', 'ADCD', 'ABAD', 'ABBD', 'ABDD', 'ABCA', 'ABCB', 'ABCC']



      I write the following python code:



      strin = 'ABCD'
      strout = set()

      tempstr1 = ''
      tempstr2 = ''
      tempstr3 = ''
      tempstr4 = ''
      for base in range(len(strin)):
      if strin[base] == 'A': #this block will be repeated for char B, C and D
      tempstr1 = strin.replace(strin[base], 'A')
      strout.add(tempstr1)
      tempstr1 = ''

      tempstr2 = strin.replace(strin[base], 'B')
      strout.add(tempstr2)
      tempstr2 = ''

      tempstr3 = strin.replace(strin[base], 'C')
      strout.add(tempseq3)
      tempstr3 = ''

      tempstr4 = strin.replace(strin[base], 'D')
      strout.add(tempseq4)
      tempstr4 = ''

      return strout


      and it works well as long as there is no repeated character (such as 'ABCD'). However, when the input string contains repeated character (such as 'AACD'), it will return less than 3n+1 string. I tried with 'AACD' string and it returns only 10 instead of 13 strings.



      Anyone can help?










      share|improve this question
















      How to create a whole list of string from one string where each string in the list containing exactly one character replacement? The string itself is consisted of only four characters (say: A, B, C, and D), so that the whole list of a string of length n would contain 3n+1 strings with exactly one character replacement.



      Example:
      inputstr = 'ABCD'
      output = ['ABCD', 'BBCD', 'CBCD', 'DBCD', 'AACD', 'ACCD', 'ADCD', 'ABAD', 'ABBD', 'ABDD', 'ABCA', 'ABCB', 'ABCC']



      I write the following python code:



      strin = 'ABCD'
      strout = set()

      tempstr1 = ''
      tempstr2 = ''
      tempstr3 = ''
      tempstr4 = ''
      for base in range(len(strin)):
      if strin[base] == 'A': #this block will be repeated for char B, C and D
      tempstr1 = strin.replace(strin[base], 'A')
      strout.add(tempstr1)
      tempstr1 = ''

      tempstr2 = strin.replace(strin[base], 'B')
      strout.add(tempstr2)
      tempstr2 = ''

      tempstr3 = strin.replace(strin[base], 'C')
      strout.add(tempseq3)
      tempstr3 = ''

      tempstr4 = strin.replace(strin[base], 'D')
      strout.add(tempseq4)
      tempstr4 = ''

      return strout


      and it works well as long as there is no repeated character (such as 'ABCD'). However, when the input string contains repeated character (such as 'AACD'), it will return less than 3n+1 string. I tried with 'AACD' string and it returns only 10 instead of 13 strings.



      Anyone can help?







      python-3.x






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 4:05







      Vic Brown

















      asked Nov 23 '18 at 2:26









      Vic BrownVic Brown

      12




      12
























          2 Answers
          2






          active

          oldest

          votes


















          0














          change

          strout = set() ===> strout = list()






          share|improve this answer
























          • previously I tried strout = list(), but it resulted a list containing sting duplicates..

            – Vic Brown
            Nov 23 '18 at 4:04



















          0














          I found it. I used a slicing method to create a list of total combination of strings with one replacement.



          for i in range(len(seq)):
          seqxlist.append(seq[:i] + 'x' + seq[i+1:])


          and after that I filter out all the x-replaced strings which are longer than the original string length:



          seqxlist = [x for x in seqxlist if (len(x) == len(seq))]


          Then, I changed x into any of the substitution characters:



          for m in seqxlist:     
          tempseq1 = m.replace('x', 'A')
          outseq.append(tempseq1)

          tempseq2 = m.replace('x', 'B')
          outseq.append(tempseq2)

          tempseq3 = m.replace('x', 'C')
          outseq.append(tempseq3)

          tempseq4 = m.replace('x', 'D')
          outseq.append(tempseq4)


          This will create all the possible combinations of string replacement, but still contains duplicates. To remove duplicates, I use set() to the outseq list.






          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%2f53439960%2fcreate-a-list-of-strings-with-one-multiple-character-replacement%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            change

            strout = set() ===> strout = list()






            share|improve this answer
























            • previously I tried strout = list(), but it resulted a list containing sting duplicates..

              – Vic Brown
              Nov 23 '18 at 4:04
















            0














            change

            strout = set() ===> strout = list()






            share|improve this answer
























            • previously I tried strout = list(), but it resulted a list containing sting duplicates..

              – Vic Brown
              Nov 23 '18 at 4:04














            0












            0








            0







            change

            strout = set() ===> strout = list()






            share|improve this answer













            change

            strout = set() ===> strout = list()







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 23 '18 at 2:34









            Hawl HeHawl He

            1




            1













            • previously I tried strout = list(), but it resulted a list containing sting duplicates..

              – Vic Brown
              Nov 23 '18 at 4:04



















            • previously I tried strout = list(), but it resulted a list containing sting duplicates..

              – Vic Brown
              Nov 23 '18 at 4:04

















            previously I tried strout = list(), but it resulted a list containing sting duplicates..

            – Vic Brown
            Nov 23 '18 at 4:04





            previously I tried strout = list(), but it resulted a list containing sting duplicates..

            – Vic Brown
            Nov 23 '18 at 4:04













            0














            I found it. I used a slicing method to create a list of total combination of strings with one replacement.



            for i in range(len(seq)):
            seqxlist.append(seq[:i] + 'x' + seq[i+1:])


            and after that I filter out all the x-replaced strings which are longer than the original string length:



            seqxlist = [x for x in seqxlist if (len(x) == len(seq))]


            Then, I changed x into any of the substitution characters:



            for m in seqxlist:     
            tempseq1 = m.replace('x', 'A')
            outseq.append(tempseq1)

            tempseq2 = m.replace('x', 'B')
            outseq.append(tempseq2)

            tempseq3 = m.replace('x', 'C')
            outseq.append(tempseq3)

            tempseq4 = m.replace('x', 'D')
            outseq.append(tempseq4)


            This will create all the possible combinations of string replacement, but still contains duplicates. To remove duplicates, I use set() to the outseq list.






            share|improve this answer




























              0














              I found it. I used a slicing method to create a list of total combination of strings with one replacement.



              for i in range(len(seq)):
              seqxlist.append(seq[:i] + 'x' + seq[i+1:])


              and after that I filter out all the x-replaced strings which are longer than the original string length:



              seqxlist = [x for x in seqxlist if (len(x) == len(seq))]


              Then, I changed x into any of the substitution characters:



              for m in seqxlist:     
              tempseq1 = m.replace('x', 'A')
              outseq.append(tempseq1)

              tempseq2 = m.replace('x', 'B')
              outseq.append(tempseq2)

              tempseq3 = m.replace('x', 'C')
              outseq.append(tempseq3)

              tempseq4 = m.replace('x', 'D')
              outseq.append(tempseq4)


              This will create all the possible combinations of string replacement, but still contains duplicates. To remove duplicates, I use set() to the outseq list.






              share|improve this answer


























                0












                0








                0







                I found it. I used a slicing method to create a list of total combination of strings with one replacement.



                for i in range(len(seq)):
                seqxlist.append(seq[:i] + 'x' + seq[i+1:])


                and after that I filter out all the x-replaced strings which are longer than the original string length:



                seqxlist = [x for x in seqxlist if (len(x) == len(seq))]


                Then, I changed x into any of the substitution characters:



                for m in seqxlist:     
                tempseq1 = m.replace('x', 'A')
                outseq.append(tempseq1)

                tempseq2 = m.replace('x', 'B')
                outseq.append(tempseq2)

                tempseq3 = m.replace('x', 'C')
                outseq.append(tempseq3)

                tempseq4 = m.replace('x', 'D')
                outseq.append(tempseq4)


                This will create all the possible combinations of string replacement, but still contains duplicates. To remove duplicates, I use set() to the outseq list.






                share|improve this answer













                I found it. I used a slicing method to create a list of total combination of strings with one replacement.



                for i in range(len(seq)):
                seqxlist.append(seq[:i] + 'x' + seq[i+1:])


                and after that I filter out all the x-replaced strings which are longer than the original string length:



                seqxlist = [x for x in seqxlist if (len(x) == len(seq))]


                Then, I changed x into any of the substitution characters:



                for m in seqxlist:     
                tempseq1 = m.replace('x', 'A')
                outseq.append(tempseq1)

                tempseq2 = m.replace('x', 'B')
                outseq.append(tempseq2)

                tempseq3 = m.replace('x', 'C')
                outseq.append(tempseq3)

                tempseq4 = m.replace('x', 'D')
                outseq.append(tempseq4)


                This will create all the possible combinations of string replacement, but still contains duplicates. To remove duplicates, I use set() to the outseq list.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 10 at 13:29









                Vic BrownVic Brown

                12




                12






























                    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%2f53439960%2fcreate-a-list-of-strings-with-one-multiple-character-replacement%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

                    Futebolista

                    Feedback on college project

                    Albești (Vaslui)