Specific list manipulation
$begingroup$
Suppose I have two lists each of different size, e.g toy case ListX = {x1,x2,x3}
and ListY = {y1,y2}
and all possible combinations of elements give rise to an element in another list ListZ = {z1,z2,z3,z4,z5,z6}
of dimension dim(ListX) x dim(ListY), i.e the tuple (x1,y1)
is associated with z1
, say.
How to merge all three lists such that I obtain the following combined list?
{{{x1,y1},z1}, {{x1,y2},z2}, {{x2,y1},z3}, {{x2,y2},z4}, {{x3,y1},z5},{{x3,y2},z6}}
I've tried nested tables but I always generate additional items that I don't want. The actual lists I'm dealing with are of dimension O(50) such that the equivalent of ListZ is of dimension O(2500).
list-manipulation
$endgroup$
add a comment |
$begingroup$
Suppose I have two lists each of different size, e.g toy case ListX = {x1,x2,x3}
and ListY = {y1,y2}
and all possible combinations of elements give rise to an element in another list ListZ = {z1,z2,z3,z4,z5,z6}
of dimension dim(ListX) x dim(ListY), i.e the tuple (x1,y1)
is associated with z1
, say.
How to merge all three lists such that I obtain the following combined list?
{{{x1,y1},z1}, {{x1,y2},z2}, {{x2,y1},z3}, {{x2,y2},z4}, {{x3,y1},z5},{{x3,y2},z6}}
I've tried nested tables but I always generate additional items that I don't want. The actual lists I'm dealing with are of dimension O(50) such that the equivalent of ListZ is of dimension O(2500).
list-manipulation
$endgroup$
add a comment |
$begingroup$
Suppose I have two lists each of different size, e.g toy case ListX = {x1,x2,x3}
and ListY = {y1,y2}
and all possible combinations of elements give rise to an element in another list ListZ = {z1,z2,z3,z4,z5,z6}
of dimension dim(ListX) x dim(ListY), i.e the tuple (x1,y1)
is associated with z1
, say.
How to merge all three lists such that I obtain the following combined list?
{{{x1,y1},z1}, {{x1,y2},z2}, {{x2,y1},z3}, {{x2,y2},z4}, {{x3,y1},z5},{{x3,y2},z6}}
I've tried nested tables but I always generate additional items that I don't want. The actual lists I'm dealing with are of dimension O(50) such that the equivalent of ListZ is of dimension O(2500).
list-manipulation
$endgroup$
Suppose I have two lists each of different size, e.g toy case ListX = {x1,x2,x3}
and ListY = {y1,y2}
and all possible combinations of elements give rise to an element in another list ListZ = {z1,z2,z3,z4,z5,z6}
of dimension dim(ListX) x dim(ListY), i.e the tuple (x1,y1)
is associated with z1
, say.
How to merge all three lists such that I obtain the following combined list?
{{{x1,y1},z1}, {{x1,y2},z2}, {{x2,y1},z3}, {{x2,y2},z4}, {{x3,y1},z5},{{x3,y2},z6}}
I've tried nested tables but I always generate additional items that I don't want. The actual lists I'm dealing with are of dimension O(50) such that the equivalent of ListZ is of dimension O(2500).
list-manipulation
list-manipulation
edited 11 mins ago
J. M. is computer-less♦
96.8k10303462
96.8k10303462
asked 8 hours ago
CAFCAF
255110
255110
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
This seems to do what you are looking for:
ListX = {x1, x2, x3};
ListY = {y1, y2};
ListZ = {z1, z2, z3, z4, z5, z6};
Partition[Riffle[Flatten[Outer[List, ListX, ListY], 1], ListZ], 2]
The Outer product gives all the pairs, but they are nested funny so you need to Flatten. Riffle does the interleaving of the Outer product with ListZ, and again it's not quite got your desired structure, but Partition fixes that.
$endgroup$
add a comment |
$begingroup$
Transpose@{Tuples@{ListX, ListY}, ListZ}
New contributor
$endgroup$
1
$begingroup$
Very nice solution! You can also useThread
instead ofTranspose
:Thread@{Tuples@{ListX, ListY}, ListZ}
$endgroup$
– rmw
7 hours ago
$begingroup$
Yes. Thanks for reminding me that.
$endgroup$
– ukar
4 hours ago
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%2f192128%2fspecific-list-manipulation%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
$begingroup$
This seems to do what you are looking for:
ListX = {x1, x2, x3};
ListY = {y1, y2};
ListZ = {z1, z2, z3, z4, z5, z6};
Partition[Riffle[Flatten[Outer[List, ListX, ListY], 1], ListZ], 2]
The Outer product gives all the pairs, but they are nested funny so you need to Flatten. Riffle does the interleaving of the Outer product with ListZ, and again it's not quite got your desired structure, but Partition fixes that.
$endgroup$
add a comment |
$begingroup$
This seems to do what you are looking for:
ListX = {x1, x2, x3};
ListY = {y1, y2};
ListZ = {z1, z2, z3, z4, z5, z6};
Partition[Riffle[Flatten[Outer[List, ListX, ListY], 1], ListZ], 2]
The Outer product gives all the pairs, but they are nested funny so you need to Flatten. Riffle does the interleaving of the Outer product with ListZ, and again it's not quite got your desired structure, but Partition fixes that.
$endgroup$
add a comment |
$begingroup$
This seems to do what you are looking for:
ListX = {x1, x2, x3};
ListY = {y1, y2};
ListZ = {z1, z2, z3, z4, z5, z6};
Partition[Riffle[Flatten[Outer[List, ListX, ListY], 1], ListZ], 2]
The Outer product gives all the pairs, but they are nested funny so you need to Flatten. Riffle does the interleaving of the Outer product with ListZ, and again it's not quite got your desired structure, but Partition fixes that.
$endgroup$
This seems to do what you are looking for:
ListX = {x1, x2, x3};
ListY = {y1, y2};
ListZ = {z1, z2, z3, z4, z5, z6};
Partition[Riffle[Flatten[Outer[List, ListX, ListY], 1], ListZ], 2]
The Outer product gives all the pairs, but they are nested funny so you need to Flatten. Riffle does the interleaving of the Outer product with ListZ, and again it's not quite got your desired structure, but Partition fixes that.
answered 8 hours ago
bill sbill s
53.6k376152
53.6k376152
add a comment |
add a comment |
$begingroup$
Transpose@{Tuples@{ListX, ListY}, ListZ}
New contributor
$endgroup$
1
$begingroup$
Very nice solution! You can also useThread
instead ofTranspose
:Thread@{Tuples@{ListX, ListY}, ListZ}
$endgroup$
– rmw
7 hours ago
$begingroup$
Yes. Thanks for reminding me that.
$endgroup$
– ukar
4 hours ago
add a comment |
$begingroup$
Transpose@{Tuples@{ListX, ListY}, ListZ}
New contributor
$endgroup$
1
$begingroup$
Very nice solution! You can also useThread
instead ofTranspose
:Thread@{Tuples@{ListX, ListY}, ListZ}
$endgroup$
– rmw
7 hours ago
$begingroup$
Yes. Thanks for reminding me that.
$endgroup$
– ukar
4 hours ago
add a comment |
$begingroup$
Transpose@{Tuples@{ListX, ListY}, ListZ}
New contributor
$endgroup$
Transpose@{Tuples@{ListX, ListY}, ListZ}
New contributor
New contributor
answered 7 hours ago
ukarukar
691
691
New contributor
New contributor
1
$begingroup$
Very nice solution! You can also useThread
instead ofTranspose
:Thread@{Tuples@{ListX, ListY}, ListZ}
$endgroup$
– rmw
7 hours ago
$begingroup$
Yes. Thanks for reminding me that.
$endgroup$
– ukar
4 hours ago
add a comment |
1
$begingroup$
Very nice solution! You can also useThread
instead ofTranspose
:Thread@{Tuples@{ListX, ListY}, ListZ}
$endgroup$
– rmw
7 hours ago
$begingroup$
Yes. Thanks for reminding me that.
$endgroup$
– ukar
4 hours ago
1
1
$begingroup$
Very nice solution! You can also use
Thread
instead of Transpose
:Thread@{Tuples@{ListX, ListY}, ListZ}
$endgroup$
– rmw
7 hours ago
$begingroup$
Very nice solution! You can also use
Thread
instead of Transpose
:Thread@{Tuples@{ListX, ListY}, ListZ}
$endgroup$
– rmw
7 hours ago
$begingroup$
Yes. Thanks for reminding me that.
$endgroup$
– ukar
4 hours ago
$begingroup$
Yes. Thanks for reminding me that.
$endgroup$
– ukar
4 hours ago
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.
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%2f192128%2fspecific-list-manipulation%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