Python: How do I randomly mix two lists?












2















If I generate two random lists for example:



N=5
X=20

parent1 = np.random.choice(X, N, replace=True)
parent2 = np.random.choice(X, N, replace=True)


Would give two lists e.g:



[2,5,1,1,12]
[3,18,4,5,1]


How could I make a new list which is a random mix of the two lists with the same amount of numbers?



e.g.



[2,18,1,5,12]


or



[18,5,1,12,5]


It doesn't matter about order.










share|improve this question























  • Is it allowed to pick same number multiple times?

    – Krishna
    Nov 22 '18 at 2:03











  • random.sample(a+b, N) would work?

    – bro-grammer
    Nov 22 '18 at 2:06











  • In a loop; randomly generate a one or a zero; if it is a one pick a random choice from the first list; else pick a random choice from the second list; stop when the item limit is reached,

    – wwii
    Nov 22 '18 at 2:07











  • Are the parents necessarily the same length?

    – Mark_Anderson
    Nov 22 '18 at 2:25
















2















If I generate two random lists for example:



N=5
X=20

parent1 = np.random.choice(X, N, replace=True)
parent2 = np.random.choice(X, N, replace=True)


Would give two lists e.g:



[2,5,1,1,12]
[3,18,4,5,1]


How could I make a new list which is a random mix of the two lists with the same amount of numbers?



e.g.



[2,18,1,5,12]


or



[18,5,1,12,5]


It doesn't matter about order.










share|improve this question























  • Is it allowed to pick same number multiple times?

    – Krishna
    Nov 22 '18 at 2:03











  • random.sample(a+b, N) would work?

    – bro-grammer
    Nov 22 '18 at 2:06











  • In a loop; randomly generate a one or a zero; if it is a one pick a random choice from the first list; else pick a random choice from the second list; stop when the item limit is reached,

    – wwii
    Nov 22 '18 at 2:07











  • Are the parents necessarily the same length?

    – Mark_Anderson
    Nov 22 '18 at 2:25














2












2








2








If I generate two random lists for example:



N=5
X=20

parent1 = np.random.choice(X, N, replace=True)
parent2 = np.random.choice(X, N, replace=True)


Would give two lists e.g:



[2,5,1,1,12]
[3,18,4,5,1]


How could I make a new list which is a random mix of the two lists with the same amount of numbers?



e.g.



[2,18,1,5,12]


or



[18,5,1,12,5]


It doesn't matter about order.










share|improve this question














If I generate two random lists for example:



N=5
X=20

parent1 = np.random.choice(X, N, replace=True)
parent2 = np.random.choice(X, N, replace=True)


Would give two lists e.g:



[2,5,1,1,12]
[3,18,4,5,1]


How could I make a new list which is a random mix of the two lists with the same amount of numbers?



e.g.



[2,18,1,5,12]


or



[18,5,1,12,5]


It doesn't matter about order.







python python-3.x list random genetic-algorithm






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 2:00









P erryP erry

455




455













  • Is it allowed to pick same number multiple times?

    – Krishna
    Nov 22 '18 at 2:03











  • random.sample(a+b, N) would work?

    – bro-grammer
    Nov 22 '18 at 2:06











  • In a loop; randomly generate a one or a zero; if it is a one pick a random choice from the first list; else pick a random choice from the second list; stop when the item limit is reached,

    – wwii
    Nov 22 '18 at 2:07











  • Are the parents necessarily the same length?

    – Mark_Anderson
    Nov 22 '18 at 2:25



















  • Is it allowed to pick same number multiple times?

    – Krishna
    Nov 22 '18 at 2:03











  • random.sample(a+b, N) would work?

    – bro-grammer
    Nov 22 '18 at 2:06











  • In a loop; randomly generate a one or a zero; if it is a one pick a random choice from the first list; else pick a random choice from the second list; stop when the item limit is reached,

    – wwii
    Nov 22 '18 at 2:07











  • Are the parents necessarily the same length?

    – Mark_Anderson
    Nov 22 '18 at 2:25

















Is it allowed to pick same number multiple times?

– Krishna
Nov 22 '18 at 2:03





Is it allowed to pick same number multiple times?

– Krishna
Nov 22 '18 at 2:03













random.sample(a+b, N) would work?

– bro-grammer
Nov 22 '18 at 2:06





random.sample(a+b, N) would work?

– bro-grammer
Nov 22 '18 at 2:06













In a loop; randomly generate a one or a zero; if it is a one pick a random choice from the first list; else pick a random choice from the second list; stop when the item limit is reached,

– wwii
Nov 22 '18 at 2:07





In a loop; randomly generate a one or a zero; if it is a one pick a random choice from the first list; else pick a random choice from the second list; stop when the item limit is reached,

– wwii
Nov 22 '18 at 2:07













Are the parents necessarily the same length?

– Mark_Anderson
Nov 22 '18 at 2:25





Are the parents necessarily the same length?

– Mark_Anderson
Nov 22 '18 at 2:25












4 Answers
4






active

oldest

votes


















4














Continuing your example, you could simply try this:



result = np.random.choice(np.concatenate([parent1,parent2]), N, replace=False)


Wether to sample with replacement or not is your choice (argument replace).






share|improve this answer


























  • parent1+parent2 will add corresponding elements of two arrays!

    – bro-grammer
    Nov 22 '18 at 2:11











  • @bro-grammer You're right. Forgot that parent1 and parent2 were arrays and not lists. Fixed it by concatenating them.

    – normanius
    Nov 22 '18 at 2:16











  • Brill thanks very much!

    – P erry
    Nov 22 '18 at 2:29



















0














if you are given parent1 and parent2, you can use np.choose:



which_one = np.random.int(2, size=N).astype(np.bool)
out_array = np.choose(which_one, [parent1, parent2])


however, keep in mind that if parent1 and parent2 are both random as per in your code, then you could simply do



out_array = np.random.choice(X, N, replace=True)





share|improve this answer































    0














    A few ways to get a uniform random selection from 2 lists.




    • Random 0 or 1 to pick list, then chose randomly in list.

    • Random number up to the sum of the lists, then use logic to chose from the first or second list according to that number.


    .



    import random
    #### Option 1
    for i in range(len(parent1)):
    list_number = random.randint(1,3)
    output(i) = exec('np.random.choice(parent'+str(list_number)+')')

    #### Option 2
    output_list = np.random.choice(np.concatenate([parent1,parent2]), N, replace=False)


    The former represents each list equally in the output regardless of their relative length, the latter samples according to the relative length of the lists.






    share|improve this answer


























    • Option 2 is so simple and exactly what I was looking for. Thanks! :)

      – P erry
      Nov 22 '18 at 2:28











    • What does "replace=False" actually mean?

      – P erry
      Nov 22 '18 at 2:28











    • When repeatedly picking from the list [parent1, parent2], do you "replace" the numbers you've picked (such that they can be picked again), or not?

      – Mark_Anderson
      Nov 22 '18 at 2:34













    • No same number can be used twice unless there is a duplicate of the number

      – P erry
      Nov 22 '18 at 11:01











    • Sorry, that was my explanation of Replace=False. The code above does as you require.

      – Mark_Anderson
      Nov 22 '18 at 12:55



















    0














    One way would be:



    N=5
    select_one = np.random.randint(0,2,N) # Outputs an array of 1 or 0s
    mixed_list = select_one*parent1 + (1 - select_one)*parent2


    If you also want to randomly permute the list elements, you can use:



    mixed_list = np.random.permutation(rand_fact*parent1 + (1 - rand_fact)*parent2)





    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%2f53422887%2fpython-how-do-i-randomly-mix-two-lists%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      Continuing your example, you could simply try this:



      result = np.random.choice(np.concatenate([parent1,parent2]), N, replace=False)


      Wether to sample with replacement or not is your choice (argument replace).






      share|improve this answer


























      • parent1+parent2 will add corresponding elements of two arrays!

        – bro-grammer
        Nov 22 '18 at 2:11











      • @bro-grammer You're right. Forgot that parent1 and parent2 were arrays and not lists. Fixed it by concatenating them.

        – normanius
        Nov 22 '18 at 2:16











      • Brill thanks very much!

        – P erry
        Nov 22 '18 at 2:29
















      4














      Continuing your example, you could simply try this:



      result = np.random.choice(np.concatenate([parent1,parent2]), N, replace=False)


      Wether to sample with replacement or not is your choice (argument replace).






      share|improve this answer


























      • parent1+parent2 will add corresponding elements of two arrays!

        – bro-grammer
        Nov 22 '18 at 2:11











      • @bro-grammer You're right. Forgot that parent1 and parent2 were arrays and not lists. Fixed it by concatenating them.

        – normanius
        Nov 22 '18 at 2:16











      • Brill thanks very much!

        – P erry
        Nov 22 '18 at 2:29














      4












      4








      4







      Continuing your example, you could simply try this:



      result = np.random.choice(np.concatenate([parent1,parent2]), N, replace=False)


      Wether to sample with replacement or not is your choice (argument replace).






      share|improve this answer















      Continuing your example, you could simply try this:



      result = np.random.choice(np.concatenate([parent1,parent2]), N, replace=False)


      Wether to sample with replacement or not is your choice (argument replace).







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 22 '18 at 2:31

























      answered Nov 22 '18 at 2:06









      normaniusnormanius

      1,4861129




      1,4861129













      • parent1+parent2 will add corresponding elements of two arrays!

        – bro-grammer
        Nov 22 '18 at 2:11











      • @bro-grammer You're right. Forgot that parent1 and parent2 were arrays and not lists. Fixed it by concatenating them.

        – normanius
        Nov 22 '18 at 2:16











      • Brill thanks very much!

        – P erry
        Nov 22 '18 at 2:29



















      • parent1+parent2 will add corresponding elements of two arrays!

        – bro-grammer
        Nov 22 '18 at 2:11











      • @bro-grammer You're right. Forgot that parent1 and parent2 were arrays and not lists. Fixed it by concatenating them.

        – normanius
        Nov 22 '18 at 2:16











      • Brill thanks very much!

        – P erry
        Nov 22 '18 at 2:29

















      parent1+parent2 will add corresponding elements of two arrays!

      – bro-grammer
      Nov 22 '18 at 2:11





      parent1+parent2 will add corresponding elements of two arrays!

      – bro-grammer
      Nov 22 '18 at 2:11













      @bro-grammer You're right. Forgot that parent1 and parent2 were arrays and not lists. Fixed it by concatenating them.

      – normanius
      Nov 22 '18 at 2:16





      @bro-grammer You're right. Forgot that parent1 and parent2 were arrays and not lists. Fixed it by concatenating them.

      – normanius
      Nov 22 '18 at 2:16













      Brill thanks very much!

      – P erry
      Nov 22 '18 at 2:29





      Brill thanks very much!

      – P erry
      Nov 22 '18 at 2:29













      0














      if you are given parent1 and parent2, you can use np.choose:



      which_one = np.random.int(2, size=N).astype(np.bool)
      out_array = np.choose(which_one, [parent1, parent2])


      however, keep in mind that if parent1 and parent2 are both random as per in your code, then you could simply do



      out_array = np.random.choice(X, N, replace=True)





      share|improve this answer




























        0














        if you are given parent1 and parent2, you can use np.choose:



        which_one = np.random.int(2, size=N).astype(np.bool)
        out_array = np.choose(which_one, [parent1, parent2])


        however, keep in mind that if parent1 and parent2 are both random as per in your code, then you could simply do



        out_array = np.random.choice(X, N, replace=True)





        share|improve this answer


























          0












          0








          0







          if you are given parent1 and parent2, you can use np.choose:



          which_one = np.random.int(2, size=N).astype(np.bool)
          out_array = np.choose(which_one, [parent1, parent2])


          however, keep in mind that if parent1 and parent2 are both random as per in your code, then you could simply do



          out_array = np.random.choice(X, N, replace=True)





          share|improve this answer













          if you are given parent1 and parent2, you can use np.choose:



          which_one = np.random.int(2, size=N).astype(np.bool)
          out_array = np.choose(which_one, [parent1, parent2])


          however, keep in mind that if parent1 and parent2 are both random as per in your code, then you could simply do



          out_array = np.random.choice(X, N, replace=True)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 2:14









          ealfieealfie

          1




          1























              0














              A few ways to get a uniform random selection from 2 lists.




              • Random 0 or 1 to pick list, then chose randomly in list.

              • Random number up to the sum of the lists, then use logic to chose from the first or second list according to that number.


              .



              import random
              #### Option 1
              for i in range(len(parent1)):
              list_number = random.randint(1,3)
              output(i) = exec('np.random.choice(parent'+str(list_number)+')')

              #### Option 2
              output_list = np.random.choice(np.concatenate([parent1,parent2]), N, replace=False)


              The former represents each list equally in the output regardless of their relative length, the latter samples according to the relative length of the lists.






              share|improve this answer


























              • Option 2 is so simple and exactly what I was looking for. Thanks! :)

                – P erry
                Nov 22 '18 at 2:28











              • What does "replace=False" actually mean?

                – P erry
                Nov 22 '18 at 2:28











              • When repeatedly picking from the list [parent1, parent2], do you "replace" the numbers you've picked (such that they can be picked again), or not?

                – Mark_Anderson
                Nov 22 '18 at 2:34













              • No same number can be used twice unless there is a duplicate of the number

                – P erry
                Nov 22 '18 at 11:01











              • Sorry, that was my explanation of Replace=False. The code above does as you require.

                – Mark_Anderson
                Nov 22 '18 at 12:55
















              0














              A few ways to get a uniform random selection from 2 lists.




              • Random 0 or 1 to pick list, then chose randomly in list.

              • Random number up to the sum of the lists, then use logic to chose from the first or second list according to that number.


              .



              import random
              #### Option 1
              for i in range(len(parent1)):
              list_number = random.randint(1,3)
              output(i) = exec('np.random.choice(parent'+str(list_number)+')')

              #### Option 2
              output_list = np.random.choice(np.concatenate([parent1,parent2]), N, replace=False)


              The former represents each list equally in the output regardless of their relative length, the latter samples according to the relative length of the lists.






              share|improve this answer


























              • Option 2 is so simple and exactly what I was looking for. Thanks! :)

                – P erry
                Nov 22 '18 at 2:28











              • What does "replace=False" actually mean?

                – P erry
                Nov 22 '18 at 2:28











              • When repeatedly picking from the list [parent1, parent2], do you "replace" the numbers you've picked (such that they can be picked again), or not?

                – Mark_Anderson
                Nov 22 '18 at 2:34













              • No same number can be used twice unless there is a duplicate of the number

                – P erry
                Nov 22 '18 at 11:01











              • Sorry, that was my explanation of Replace=False. The code above does as you require.

                – Mark_Anderson
                Nov 22 '18 at 12:55














              0












              0








              0







              A few ways to get a uniform random selection from 2 lists.




              • Random 0 or 1 to pick list, then chose randomly in list.

              • Random number up to the sum of the lists, then use logic to chose from the first or second list according to that number.


              .



              import random
              #### Option 1
              for i in range(len(parent1)):
              list_number = random.randint(1,3)
              output(i) = exec('np.random.choice(parent'+str(list_number)+')')

              #### Option 2
              output_list = np.random.choice(np.concatenate([parent1,parent2]), N, replace=False)


              The former represents each list equally in the output regardless of their relative length, the latter samples according to the relative length of the lists.






              share|improve this answer















              A few ways to get a uniform random selection from 2 lists.




              • Random 0 or 1 to pick list, then chose randomly in list.

              • Random number up to the sum of the lists, then use logic to chose from the first or second list according to that number.


              .



              import random
              #### Option 1
              for i in range(len(parent1)):
              list_number = random.randint(1,3)
              output(i) = exec('np.random.choice(parent'+str(list_number)+')')

              #### Option 2
              output_list = np.random.choice(np.concatenate([parent1,parent2]), N, replace=False)


              The former represents each list equally in the output regardless of their relative length, the latter samples according to the relative length of the lists.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 22 '18 at 2:18

























              answered Nov 22 '18 at 2:06









              Mark_AndersonMark_Anderson

              421216




              421216













              • Option 2 is so simple and exactly what I was looking for. Thanks! :)

                – P erry
                Nov 22 '18 at 2:28











              • What does "replace=False" actually mean?

                – P erry
                Nov 22 '18 at 2:28











              • When repeatedly picking from the list [parent1, parent2], do you "replace" the numbers you've picked (such that they can be picked again), or not?

                – Mark_Anderson
                Nov 22 '18 at 2:34













              • No same number can be used twice unless there is a duplicate of the number

                – P erry
                Nov 22 '18 at 11:01











              • Sorry, that was my explanation of Replace=False. The code above does as you require.

                – Mark_Anderson
                Nov 22 '18 at 12:55



















              • Option 2 is so simple and exactly what I was looking for. Thanks! :)

                – P erry
                Nov 22 '18 at 2:28











              • What does "replace=False" actually mean?

                – P erry
                Nov 22 '18 at 2:28











              • When repeatedly picking from the list [parent1, parent2], do you "replace" the numbers you've picked (such that they can be picked again), or not?

                – Mark_Anderson
                Nov 22 '18 at 2:34













              • No same number can be used twice unless there is a duplicate of the number

                – P erry
                Nov 22 '18 at 11:01











              • Sorry, that was my explanation of Replace=False. The code above does as you require.

                – Mark_Anderson
                Nov 22 '18 at 12:55

















              Option 2 is so simple and exactly what I was looking for. Thanks! :)

              – P erry
              Nov 22 '18 at 2:28





              Option 2 is so simple and exactly what I was looking for. Thanks! :)

              – P erry
              Nov 22 '18 at 2:28













              What does "replace=False" actually mean?

              – P erry
              Nov 22 '18 at 2:28





              What does "replace=False" actually mean?

              – P erry
              Nov 22 '18 at 2:28













              When repeatedly picking from the list [parent1, parent2], do you "replace" the numbers you've picked (such that they can be picked again), or not?

              – Mark_Anderson
              Nov 22 '18 at 2:34







              When repeatedly picking from the list [parent1, parent2], do you "replace" the numbers you've picked (such that they can be picked again), or not?

              – Mark_Anderson
              Nov 22 '18 at 2:34















              No same number can be used twice unless there is a duplicate of the number

              – P erry
              Nov 22 '18 at 11:01





              No same number can be used twice unless there is a duplicate of the number

              – P erry
              Nov 22 '18 at 11:01













              Sorry, that was my explanation of Replace=False. The code above does as you require.

              – Mark_Anderson
              Nov 22 '18 at 12:55





              Sorry, that was my explanation of Replace=False. The code above does as you require.

              – Mark_Anderson
              Nov 22 '18 at 12:55











              0














              One way would be:



              N=5
              select_one = np.random.randint(0,2,N) # Outputs an array of 1 or 0s
              mixed_list = select_one*parent1 + (1 - select_one)*parent2


              If you also want to randomly permute the list elements, you can use:



              mixed_list = np.random.permutation(rand_fact*parent1 + (1 - rand_fact)*parent2)





              share|improve this answer




























                0














                One way would be:



                N=5
                select_one = np.random.randint(0,2,N) # Outputs an array of 1 or 0s
                mixed_list = select_one*parent1 + (1 - select_one)*parent2


                If you also want to randomly permute the list elements, you can use:



                mixed_list = np.random.permutation(rand_fact*parent1 + (1 - rand_fact)*parent2)





                share|improve this answer


























                  0












                  0








                  0







                  One way would be:



                  N=5
                  select_one = np.random.randint(0,2,N) # Outputs an array of 1 or 0s
                  mixed_list = select_one*parent1 + (1 - select_one)*parent2


                  If you also want to randomly permute the list elements, you can use:



                  mixed_list = np.random.permutation(rand_fact*parent1 + (1 - rand_fact)*parent2)





                  share|improve this answer













                  One way would be:



                  N=5
                  select_one = np.random.randint(0,2,N) # Outputs an array of 1 or 0s
                  mixed_list = select_one*parent1 + (1 - select_one)*parent2


                  If you also want to randomly permute the list elements, you can use:



                  mixed_list = np.random.permutation(rand_fact*parent1 + (1 - rand_fact)*parent2)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 2:20









                  RR_28023RR_28023

                  134




                  134






























                      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%2f53422887%2fpython-how-do-i-randomly-mix-two-lists%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

                      Create new schema in PostgreSQL using DBeaver

                      Deepest pit of an array with Javascript: test on Codility

                      Costa Masnaga