Basis enumeration of large lists
up vote
3
down vote
favorite
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 |
up vote
3
down vote
favorite
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 |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
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 at 8:01
Henrik Schumacher
46.6k466133
46.6k466133
asked Dec 3 at 7:20
Hubble07
2,858717
2,858717
add a comment |
add a comment |
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}}
add a comment |
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
add a comment |
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}}
add a comment |
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}}
add a comment |
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}}
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 at 7:54
answered Dec 3 at 7:45
kglr
175k9197402
175k9197402
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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 at 9:00
answered Dec 3 at 7:59
Henrik Schumacher
46.6k466133
46.6k466133
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