Basis enumeration of large lists











up vote
3
down vote

favorite
1












I want to combine two list in the following way without using Table or any loop structure.



genList[n_] := Table[RandomInteger[{1, 10}, 4], n]  (* list generating function*)


The two lists that needs to be combined are:-



list1 = genList[3]
list2 = genList[4]


What I want to achieve is as follows:-



Partition[
Flatten[
Table[{list1[[i]], list2[[j]]}, {i, 1
Length[list1]},{j,1 Length[list2]}]
]
,8]


So I simply need to enumerate each of the elements of list 1 combined with all of the elements of list 2.



What would be an efficient way of doing this with large lists?



Let's say with Length[list1] = 100 and Length[list2] = 200.



Also Length[list2] > Length[list1]










share|improve this question




























    up vote
    3
    down vote

    favorite
    1












    I want to combine two list in the following way without using Table or any loop structure.



    genList[n_] := Table[RandomInteger[{1, 10}, 4], n]  (* list generating function*)


    The two lists that needs to be combined are:-



    list1 = genList[3]
    list2 = genList[4]


    What I want to achieve is as follows:-



    Partition[
    Flatten[
    Table[{list1[[i]], list2[[j]]}, {i, 1
    Length[list1]},{j,1 Length[list2]}]
    ]
    ,8]


    So I simply need to enumerate each of the elements of list 1 combined with all of the elements of list 2.



    What would be an efficient way of doing this with large lists?



    Let's say with Length[list1] = 100 and Length[list2] = 200.



    Also Length[list2] > Length[list1]










    share|improve this question


























      up vote
      3
      down vote

      favorite
      1









      up vote
      3
      down vote

      favorite
      1






      1





      I want to combine two list in the following way without using Table or any loop structure.



      genList[n_] := Table[RandomInteger[{1, 10}, 4], n]  (* list generating function*)


      The two lists that needs to be combined are:-



      list1 = genList[3]
      list2 = genList[4]


      What I want to achieve is as follows:-



      Partition[
      Flatten[
      Table[{list1[[i]], list2[[j]]}, {i, 1
      Length[list1]},{j,1 Length[list2]}]
      ]
      ,8]


      So I simply need to enumerate each of the elements of list 1 combined with all of the elements of list 2.



      What would be an efficient way of doing this with large lists?



      Let's say with Length[list1] = 100 and Length[list2] = 200.



      Also Length[list2] > Length[list1]










      share|improve this question















      I want to combine two list in the following way without using Table or any loop structure.



      genList[n_] := Table[RandomInteger[{1, 10}, 4], n]  (* list generating function*)


      The two lists that needs to be combined are:-



      list1 = genList[3]
      list2 = genList[4]


      What I want to achieve is as follows:-



      Partition[
      Flatten[
      Table[{list1[[i]], list2[[j]]}, {i, 1
      Length[list1]},{j,1 Length[list2]}]
      ]
      ,8]


      So I simply need to enumerate each of the elements of list 1 combined with all of the elements of list 2.



      What would be an efficient way of doing this with large lists?



      Let's say with Length[list1] = 100 and Length[list2] = 200.



      Also Length[list2] > Length[list1]







      list-manipulation






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 3 at 8:01









      Henrik Schumacher

      46.6k466133




      46.6k466133










      asked Dec 3 at 7:20









      Hubble07

      2,858717




      2,858717






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          Join @@@ Tuples[{list1, list2}] (* or *)
          Flatten /@ Tuples[{list1, list2}] (* or *)
          Join @@ Outer[Join, list1, list2, 1]



          {{5, 3, 5, 4, 9, 2, 10, 6}, {5, 3, 5, 4, 10, 6, 4, 7},

          {5, 3, 5, 4, 2,
          1, 7, 1}, {5, 3, 5, 4, 6, 6, 1, 9},

          {2, 6, 9, 5, 9, 2, 10, 6}, {2,
          6, 9, 5, 10, 6, 4, 7},

          {2, 6, 9, 5, 2, 1, 7, 1}, {2, 6, 9, 5, 6, 6,
          1, 9}, {10, 8, 9, 6, 9, 2, 10, 6},

          {10, 8, 9, 6, 10, 6, 4, 7}, {10,
          8, 9, 6, 2, 1, 7, 1}, {10, 8, 9, 6, 6, 6, 1, 9}}







          share|improve this answer






























            up vote
            3
            down vote













            This is a variant of kglr's approach that employs ArrayFlatten instead of mapping Flatten. Notice also that I changed genList so that it produces packed arrays; this is crucial for performance.



            genList[n_] := RandomInteger[{1, 10}, {n, 4}];
            m = 100;
            n = 100;
            list1 = genList[m];
            list2 = genList[n];

            a = Partition[Flatten[Table[{list1[[i]], list2[[j]]}, {i, 1 Length[list1]}, {j, 1 Length[list2]}]], 8]; // RepeatedTiming // First
            b = Flatten /@ Tuples[{list1, list2}]; // RepeatedTiming // First
            c = ArrayReshape[
            Tuples[{list1, list2}],
            {Length[list1] Length[list2], Dimensions[list1][[2]] + Dimensions[list2][[2]]}
            ]; // RepeatedTiming // First
            a == b == c



            0.026



            0.0012



            0.000095



            True







            share|improve this answer























              Your Answer





              StackExchange.ifUsing("editor", function () {
              return StackExchange.using("mathjaxEditing", function () {
              StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
              StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
              });
              });
              }, "mathjax-editing");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "387"
              };
              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',
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              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%2fmathematica.stackexchange.com%2fquestions%2f187199%2fbasis-enumeration-of-large-lists%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








              up vote
              3
              down vote



              accepted










              Join @@@ Tuples[{list1, list2}] (* or *)
              Flatten /@ Tuples[{list1, list2}] (* or *)
              Join @@ Outer[Join, list1, list2, 1]



              {{5, 3, 5, 4, 9, 2, 10, 6}, {5, 3, 5, 4, 10, 6, 4, 7},

              {5, 3, 5, 4, 2,
              1, 7, 1}, {5, 3, 5, 4, 6, 6, 1, 9},

              {2, 6, 9, 5, 9, 2, 10, 6}, {2,
              6, 9, 5, 10, 6, 4, 7},

              {2, 6, 9, 5, 2, 1, 7, 1}, {2, 6, 9, 5, 6, 6,
              1, 9}, {10, 8, 9, 6, 9, 2, 10, 6},

              {10, 8, 9, 6, 10, 6, 4, 7}, {10,
              8, 9, 6, 2, 1, 7, 1}, {10, 8, 9, 6, 6, 6, 1, 9}}







              share|improve this answer



























                up vote
                3
                down vote



                accepted










                Join @@@ Tuples[{list1, list2}] (* or *)
                Flatten /@ Tuples[{list1, list2}] (* or *)
                Join @@ Outer[Join, list1, list2, 1]



                {{5, 3, 5, 4, 9, 2, 10, 6}, {5, 3, 5, 4, 10, 6, 4, 7},

                {5, 3, 5, 4, 2,
                1, 7, 1}, {5, 3, 5, 4, 6, 6, 1, 9},

                {2, 6, 9, 5, 9, 2, 10, 6}, {2,
                6, 9, 5, 10, 6, 4, 7},

                {2, 6, 9, 5, 2, 1, 7, 1}, {2, 6, 9, 5, 6, 6,
                1, 9}, {10, 8, 9, 6, 9, 2, 10, 6},

                {10, 8, 9, 6, 10, 6, 4, 7}, {10,
                8, 9, 6, 2, 1, 7, 1}, {10, 8, 9, 6, 6, 6, 1, 9}}







                share|improve this answer

























                  up vote
                  3
                  down vote



                  accepted







                  up vote
                  3
                  down vote



                  accepted






                  Join @@@ Tuples[{list1, list2}] (* or *)
                  Flatten /@ Tuples[{list1, list2}] (* or *)
                  Join @@ Outer[Join, list1, list2, 1]



                  {{5, 3, 5, 4, 9, 2, 10, 6}, {5, 3, 5, 4, 10, 6, 4, 7},

                  {5, 3, 5, 4, 2,
                  1, 7, 1}, {5, 3, 5, 4, 6, 6, 1, 9},

                  {2, 6, 9, 5, 9, 2, 10, 6}, {2,
                  6, 9, 5, 10, 6, 4, 7},

                  {2, 6, 9, 5, 2, 1, 7, 1}, {2, 6, 9, 5, 6, 6,
                  1, 9}, {10, 8, 9, 6, 9, 2, 10, 6},

                  {10, 8, 9, 6, 10, 6, 4, 7}, {10,
                  8, 9, 6, 2, 1, 7, 1}, {10, 8, 9, 6, 6, 6, 1, 9}}







                  share|improve this answer














                  Join @@@ Tuples[{list1, list2}] (* or *)
                  Flatten /@ Tuples[{list1, list2}] (* or *)
                  Join @@ Outer[Join, list1, list2, 1]



                  {{5, 3, 5, 4, 9, 2, 10, 6}, {5, 3, 5, 4, 10, 6, 4, 7},

                  {5, 3, 5, 4, 2,
                  1, 7, 1}, {5, 3, 5, 4, 6, 6, 1, 9},

                  {2, 6, 9, 5, 9, 2, 10, 6}, {2,
                  6, 9, 5, 10, 6, 4, 7},

                  {2, 6, 9, 5, 2, 1, 7, 1}, {2, 6, 9, 5, 6, 6,
                  1, 9}, {10, 8, 9, 6, 9, 2, 10, 6},

                  {10, 8, 9, 6, 10, 6, 4, 7}, {10,
                  8, 9, 6, 2, 1, 7, 1}, {10, 8, 9, 6, 6, 6, 1, 9}}








                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 3 at 7:54

























                  answered Dec 3 at 7:45









                  kglr

                  175k9197402




                  175k9197402






















                      up vote
                      3
                      down vote













                      This is a variant of kglr's approach that employs ArrayFlatten instead of mapping Flatten. Notice also that I changed genList so that it produces packed arrays; this is crucial for performance.



                      genList[n_] := RandomInteger[{1, 10}, {n, 4}];
                      m = 100;
                      n = 100;
                      list1 = genList[m];
                      list2 = genList[n];

                      a = Partition[Flatten[Table[{list1[[i]], list2[[j]]}, {i, 1 Length[list1]}, {j, 1 Length[list2]}]], 8]; // RepeatedTiming // First
                      b = Flatten /@ Tuples[{list1, list2}]; // RepeatedTiming // First
                      c = ArrayReshape[
                      Tuples[{list1, list2}],
                      {Length[list1] Length[list2], Dimensions[list1][[2]] + Dimensions[list2][[2]]}
                      ]; // RepeatedTiming // First
                      a == b == c



                      0.026



                      0.0012



                      0.000095



                      True







                      share|improve this answer



























                        up vote
                        3
                        down vote













                        This is a variant of kglr's approach that employs ArrayFlatten instead of mapping Flatten. Notice also that I changed genList so that it produces packed arrays; this is crucial for performance.



                        genList[n_] := RandomInteger[{1, 10}, {n, 4}];
                        m = 100;
                        n = 100;
                        list1 = genList[m];
                        list2 = genList[n];

                        a = Partition[Flatten[Table[{list1[[i]], list2[[j]]}, {i, 1 Length[list1]}, {j, 1 Length[list2]}]], 8]; // RepeatedTiming // First
                        b = Flatten /@ Tuples[{list1, list2}]; // RepeatedTiming // First
                        c = ArrayReshape[
                        Tuples[{list1, list2}],
                        {Length[list1] Length[list2], Dimensions[list1][[2]] + Dimensions[list2][[2]]}
                        ]; // RepeatedTiming // First
                        a == b == c



                        0.026



                        0.0012



                        0.000095



                        True







                        share|improve this answer

























                          up vote
                          3
                          down vote










                          up vote
                          3
                          down vote









                          This is a variant of kglr's approach that employs ArrayFlatten instead of mapping Flatten. Notice also that I changed genList so that it produces packed arrays; this is crucial for performance.



                          genList[n_] := RandomInteger[{1, 10}, {n, 4}];
                          m = 100;
                          n = 100;
                          list1 = genList[m];
                          list2 = genList[n];

                          a = Partition[Flatten[Table[{list1[[i]], list2[[j]]}, {i, 1 Length[list1]}, {j, 1 Length[list2]}]], 8]; // RepeatedTiming // First
                          b = Flatten /@ Tuples[{list1, list2}]; // RepeatedTiming // First
                          c = ArrayReshape[
                          Tuples[{list1, list2}],
                          {Length[list1] Length[list2], Dimensions[list1][[2]] + Dimensions[list2][[2]]}
                          ]; // RepeatedTiming // First
                          a == b == c



                          0.026



                          0.0012



                          0.000095



                          True







                          share|improve this answer














                          This is a variant of kglr's approach that employs ArrayFlatten instead of mapping Flatten. Notice also that I changed genList so that it produces packed arrays; this is crucial for performance.



                          genList[n_] := RandomInteger[{1, 10}, {n, 4}];
                          m = 100;
                          n = 100;
                          list1 = genList[m];
                          list2 = genList[n];

                          a = Partition[Flatten[Table[{list1[[i]], list2[[j]]}, {i, 1 Length[list1]}, {j, 1 Length[list2]}]], 8]; // RepeatedTiming // First
                          b = Flatten /@ Tuples[{list1, list2}]; // RepeatedTiming // First
                          c = ArrayReshape[
                          Tuples[{list1, list2}],
                          {Length[list1] Length[list2], Dimensions[list1][[2]] + Dimensions[list2][[2]]}
                          ]; // RepeatedTiming // First
                          a == b == c



                          0.026



                          0.0012



                          0.000095



                          True








                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Dec 3 at 9:00

























                          answered Dec 3 at 7:59









                          Henrik Schumacher

                          46.6k466133




                          46.6k466133






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Mathematica Stack Exchange!


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


                              Use MathJax to format equations. MathJax reference.


                              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%2fmathematica.stackexchange.com%2fquestions%2f187199%2fbasis-enumeration-of-large-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

                              How did Captain America manage to do this?

                              迪纳利

                              南乌拉尔铁路局