Basis enumeration of large lists
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
add a comment |
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
add a comment |
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
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
list-manipulation
edited Dec 3 '18 at 8:01
Henrik Schumacher
48.8k467139
48.8k467139
asked Dec 3 '18 at 7:20
Hubble07
2,887720
2,887720
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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}}
add a comment |
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
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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}}
add a comment |
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}}
add a comment |
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}}
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}}
edited Dec 3 '18 at 7:54
answered Dec 3 '18 at 7:45
kglr
177k9198405
177k9198405
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Dec 3 '18 at 9:00
answered Dec 3 '18 at 7:59
Henrik Schumacher
48.8k467139
48.8k467139
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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