How to Build a List from Separate ListsGiven a list of lists, build a list in which lists with equal first...
Can I legally make a website about boycotting a certain company?
Do the speed limit reductions due to pollution also apply to electric cars in France?
What does it mean for south of due west?
UK visa start date and Flight Depature Time
Does しかたない imply disappointment?
bash aliases do not expand even with shopt expand_aliases
How can changes in personality/values of a person who turned into a vampire be explained?
3D buried view in Tikz
Is there a way to pause a running process on Linux systems and resume later?
What really causes series inductance of capacitors?
Coworker asking me to not bring cakes due to self control issue. What should I do?
Custom taxonomies
How to Build a List from Separate Lists
Including proofs of known theorems in master's thesis
Whats happened with already installed GNOME apps if I install and run KDE to Ubuntu 18.04?
How to purchase a drop bar bike that will be converted to flat bar?
How can I handle players killing my NPC outside of combat?
Protagonist constantly has to have long words explained to her. Will this get tedious?
What is the smallest molar volume?
How can a Sorcerer/Warlock use 4 Eldritch blasts in one round?
In the Lost in Space intro why was Dr. Smith actor listed as a special guest star?
Can a Hydra make multiple opportunity attacks at once?
Was the Soviet N1 really capable of sending 9.6 GB/s of telemetry?
What is an explicit bijection in combinatorics?
How to Build a List from Separate Lists
Given a list of lists, build a list in which lists with equal first and last elements are grouped togetherHow to remove redundant {} from a nested list of lists?Extracting lists from list based on lengthNew nested list from three listsHow to build a matrix from blocksHow to build equations from list of listsCreate a list from other listsSelecting elements from lists of a listBuild a list from a Loop!Build matrix from smaller matrices or rearange list of lists
$begingroup$
I suspect this is a duplicate, but I can't seem to find what I'm looking for.
A routine problem I have is the following.
I have a set of data in three (or two, or more) lists:
l1={a1, a2, a3}
l2={b1, b2, b3, b4}
l3={{c1, c2, c3, c4}, {d1, d2, d3, d4}, {e1, e2, e3, e4}}
where c1
is a result under condition {a1, b1}
, c2
is a result under condition {a1, b2}
, etc.
I want to create the list:
{{a1, b1, c1}, {a1, b2, c2}, {a1, b3, c3},{a1, b4, c4}, {a2, b1, d1}, ...}
in preparation for creating a string to export to a text file.
My current solution:
Map[Transpose[{l2, #}] &, l3]
MapIndexed[Prepend[#1, l1[[#2[[1]]]]] &, %, {2}]
Flatten[%, 1]
This works, but the solution isn't intuitive to me, which makes me think there's a better way.
Is there a preferred approach for this task?
list-manipulation
New contributor
$endgroup$
add a comment |
$begingroup$
I suspect this is a duplicate, but I can't seem to find what I'm looking for.
A routine problem I have is the following.
I have a set of data in three (or two, or more) lists:
l1={a1, a2, a3}
l2={b1, b2, b3, b4}
l3={{c1, c2, c3, c4}, {d1, d2, d3, d4}, {e1, e2, e3, e4}}
where c1
is a result under condition {a1, b1}
, c2
is a result under condition {a1, b2}
, etc.
I want to create the list:
{{a1, b1, c1}, {a1, b2, c2}, {a1, b3, c3},{a1, b4, c4}, {a2, b1, d1}, ...}
in preparation for creating a string to export to a text file.
My current solution:
Map[Transpose[{l2, #}] &, l3]
MapIndexed[Prepend[#1, l1[[#2[[1]]]]] &, %, {2}]
Flatten[%, 1]
This works, but the solution isn't intuitive to me, which makes me think there's a better way.
Is there a preferred approach for this task?
list-manipulation
New contributor
$endgroup$
add a comment |
$begingroup$
I suspect this is a duplicate, but I can't seem to find what I'm looking for.
A routine problem I have is the following.
I have a set of data in three (or two, or more) lists:
l1={a1, a2, a3}
l2={b1, b2, b3, b4}
l3={{c1, c2, c3, c4}, {d1, d2, d3, d4}, {e1, e2, e3, e4}}
where c1
is a result under condition {a1, b1}
, c2
is a result under condition {a1, b2}
, etc.
I want to create the list:
{{a1, b1, c1}, {a1, b2, c2}, {a1, b3, c3},{a1, b4, c4}, {a2, b1, d1}, ...}
in preparation for creating a string to export to a text file.
My current solution:
Map[Transpose[{l2, #}] &, l3]
MapIndexed[Prepend[#1, l1[[#2[[1]]]]] &, %, {2}]
Flatten[%, 1]
This works, but the solution isn't intuitive to me, which makes me think there's a better way.
Is there a preferred approach for this task?
list-manipulation
New contributor
$endgroup$
I suspect this is a duplicate, but I can't seem to find what I'm looking for.
A routine problem I have is the following.
I have a set of data in three (or two, or more) lists:
l1={a1, a2, a3}
l2={b1, b2, b3, b4}
l3={{c1, c2, c3, c4}, {d1, d2, d3, d4}, {e1, e2, e3, e4}}
where c1
is a result under condition {a1, b1}
, c2
is a result under condition {a1, b2}
, etc.
I want to create the list:
{{a1, b1, c1}, {a1, b2, c2}, {a1, b3, c3},{a1, b4, c4}, {a2, b1, d1}, ...}
in preparation for creating a string to export to a text file.
My current solution:
Map[Transpose[{l2, #}] &, l3]
MapIndexed[Prepend[#1, l1[[#2[[1]]]]] &, %, {2}]
Flatten[%, 1]
This works, but the solution isn't intuitive to me, which makes me think there's a better way.
Is there a preferred approach for this task?
list-manipulation
list-manipulation
New contributor
New contributor
edited 1 hour ago
Coolwater
15k32553
15k32553
New contributor
asked 1 hour ago
BrianBrian
212
212
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
Join[Tuples[{l1, l2}], ArrayReshape[l3, {Times @@ Dimensions[l3], 1}], 2]
{{a1, b1, c1}, {a1, b2, c2}, {a1, b3, c3}, ...}
Though if the elements of l3
are list of equal length, then ArrayReshape
/Dimension
won't work.
To avoid that problem you could write
ArrayReshape[Riffle[Tuples[{l1, l2}], #], {Length[#], 3}] &[Catenate[l3]]
$endgroup$
add a comment |
$begingroup$
This will generate a nested list, in accordance with l3
:
MapThread[Append, {Outer[List, l1, l2], l3}, 2]
{{{a1, b1, c1}, {a1, b2, c2}, {a1, b3, c3}, {a1, b4, c4}}, {{a2, b1, d1}, {a2, b2, d2}, {a2, b3, d3}, {a2, b4, d4}}, {{a3, b1, e1}, {a3, b2, e2}, {a3, b3, e3}, {a3, b4, e4}}}
Flatten
ing once will give you what you want:
Flatten[MapThread[Append, {Outer[List, l1, l2], l3}, 2], 1]
{{a1, b1, c1}, {a1, b2, c2}, {a1, b3, c3}, {a1, b4, c4}, {a2, b1, d1}, {a2, b2, d2}, {a2, b3, d3}, {a2, b4, d4}, {a3, b1, e1}, {a3, b2, e2}, {a3, b3, e3}, {a3, b4, e4}}
If you're not interested in the nested list above, then you can get straight to the result with
MapThread[Append, {Tuples[{l1, l2}], Flatten[l3]}]
{{a1, b1, c1}, {a1, b2, c2}, {a1, b3, c3}, {a1, b4, c4}, {a2, b1, d1}, {a2, b2, d2}, {a2, b3, d3}, {a2, b4, d4}, {a3, b1, e1}, {a3, b2, e2}, {a3, b3, e3}, {a3, b4, e4}}
(in effect, Flatten
ing before MapThread
ing).
$endgroup$
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
});
}
});
Brian is a new contributor. Be nice, and check out our Code of Conduct.
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%2f192020%2fhow-to-build-a-list-from-separate-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
$begingroup$
Join[Tuples[{l1, l2}], ArrayReshape[l3, {Times @@ Dimensions[l3], 1}], 2]
{{a1, b1, c1}, {a1, b2, c2}, {a1, b3, c3}, ...}
Though if the elements of l3
are list of equal length, then ArrayReshape
/Dimension
won't work.
To avoid that problem you could write
ArrayReshape[Riffle[Tuples[{l1, l2}], #], {Length[#], 3}] &[Catenate[l3]]
$endgroup$
add a comment |
$begingroup$
Join[Tuples[{l1, l2}], ArrayReshape[l3, {Times @@ Dimensions[l3], 1}], 2]
{{a1, b1, c1}, {a1, b2, c2}, {a1, b3, c3}, ...}
Though if the elements of l3
are list of equal length, then ArrayReshape
/Dimension
won't work.
To avoid that problem you could write
ArrayReshape[Riffle[Tuples[{l1, l2}], #], {Length[#], 3}] &[Catenate[l3]]
$endgroup$
add a comment |
$begingroup$
Join[Tuples[{l1, l2}], ArrayReshape[l3, {Times @@ Dimensions[l3], 1}], 2]
{{a1, b1, c1}, {a1, b2, c2}, {a1, b3, c3}, ...}
Though if the elements of l3
are list of equal length, then ArrayReshape
/Dimension
won't work.
To avoid that problem you could write
ArrayReshape[Riffle[Tuples[{l1, l2}], #], {Length[#], 3}] &[Catenate[l3]]
$endgroup$
Join[Tuples[{l1, l2}], ArrayReshape[l3, {Times @@ Dimensions[l3], 1}], 2]
{{a1, b1, c1}, {a1, b2, c2}, {a1, b3, c3}, ...}
Though if the elements of l3
are list of equal length, then ArrayReshape
/Dimension
won't work.
To avoid that problem you could write
ArrayReshape[Riffle[Tuples[{l1, l2}], #], {Length[#], 3}] &[Catenate[l3]]
edited 55 mins ago
answered 1 hour ago
CoolwaterCoolwater
15k32553
15k32553
add a comment |
add a comment |
$begingroup$
This will generate a nested list, in accordance with l3
:
MapThread[Append, {Outer[List, l1, l2], l3}, 2]
{{{a1, b1, c1}, {a1, b2, c2}, {a1, b3, c3}, {a1, b4, c4}}, {{a2, b1, d1}, {a2, b2, d2}, {a2, b3, d3}, {a2, b4, d4}}, {{a3, b1, e1}, {a3, b2, e2}, {a3, b3, e3}, {a3, b4, e4}}}
Flatten
ing once will give you what you want:
Flatten[MapThread[Append, {Outer[List, l1, l2], l3}, 2], 1]
{{a1, b1, c1}, {a1, b2, c2}, {a1, b3, c3}, {a1, b4, c4}, {a2, b1, d1}, {a2, b2, d2}, {a2, b3, d3}, {a2, b4, d4}, {a3, b1, e1}, {a3, b2, e2}, {a3, b3, e3}, {a3, b4, e4}}
If you're not interested in the nested list above, then you can get straight to the result with
MapThread[Append, {Tuples[{l1, l2}], Flatten[l3]}]
{{a1, b1, c1}, {a1, b2, c2}, {a1, b3, c3}, {a1, b4, c4}, {a2, b1, d1}, {a2, b2, d2}, {a2, b3, d3}, {a2, b4, d4}, {a3, b1, e1}, {a3, b2, e2}, {a3, b3, e3}, {a3, b4, e4}}
(in effect, Flatten
ing before MapThread
ing).
$endgroup$
add a comment |
$begingroup$
This will generate a nested list, in accordance with l3
:
MapThread[Append, {Outer[List, l1, l2], l3}, 2]
{{{a1, b1, c1}, {a1, b2, c2}, {a1, b3, c3}, {a1, b4, c4}}, {{a2, b1, d1}, {a2, b2, d2}, {a2, b3, d3}, {a2, b4, d4}}, {{a3, b1, e1}, {a3, b2, e2}, {a3, b3, e3}, {a3, b4, e4}}}
Flatten
ing once will give you what you want:
Flatten[MapThread[Append, {Outer[List, l1, l2], l3}, 2], 1]
{{a1, b1, c1}, {a1, b2, c2}, {a1, b3, c3}, {a1, b4, c4}, {a2, b1, d1}, {a2, b2, d2}, {a2, b3, d3}, {a2, b4, d4}, {a3, b1, e1}, {a3, b2, e2}, {a3, b3, e3}, {a3, b4, e4}}
If you're not interested in the nested list above, then you can get straight to the result with
MapThread[Append, {Tuples[{l1, l2}], Flatten[l3]}]
{{a1, b1, c1}, {a1, b2, c2}, {a1, b3, c3}, {a1, b4, c4}, {a2, b1, d1}, {a2, b2, d2}, {a2, b3, d3}, {a2, b4, d4}, {a3, b1, e1}, {a3, b2, e2}, {a3, b3, e3}, {a3, b4, e4}}
(in effect, Flatten
ing before MapThread
ing).
$endgroup$
add a comment |
$begingroup$
This will generate a nested list, in accordance with l3
:
MapThread[Append, {Outer[List, l1, l2], l3}, 2]
{{{a1, b1, c1}, {a1, b2, c2}, {a1, b3, c3}, {a1, b4, c4}}, {{a2, b1, d1}, {a2, b2, d2}, {a2, b3, d3}, {a2, b4, d4}}, {{a3, b1, e1}, {a3, b2, e2}, {a3, b3, e3}, {a3, b4, e4}}}
Flatten
ing once will give you what you want:
Flatten[MapThread[Append, {Outer[List, l1, l2], l3}, 2], 1]
{{a1, b1, c1}, {a1, b2, c2}, {a1, b3, c3}, {a1, b4, c4}, {a2, b1, d1}, {a2, b2, d2}, {a2, b3, d3}, {a2, b4, d4}, {a3, b1, e1}, {a3, b2, e2}, {a3, b3, e3}, {a3, b4, e4}}
If you're not interested in the nested list above, then you can get straight to the result with
MapThread[Append, {Tuples[{l1, l2}], Flatten[l3]}]
{{a1, b1, c1}, {a1, b2, c2}, {a1, b3, c3}, {a1, b4, c4}, {a2, b1, d1}, {a2, b2, d2}, {a2, b3, d3}, {a2, b4, d4}, {a3, b1, e1}, {a3, b2, e2}, {a3, b3, e3}, {a3, b4, e4}}
(in effect, Flatten
ing before MapThread
ing).
$endgroup$
This will generate a nested list, in accordance with l3
:
MapThread[Append, {Outer[List, l1, l2], l3}, 2]
{{{a1, b1, c1}, {a1, b2, c2}, {a1, b3, c3}, {a1, b4, c4}}, {{a2, b1, d1}, {a2, b2, d2}, {a2, b3, d3}, {a2, b4, d4}}, {{a3, b1, e1}, {a3, b2, e2}, {a3, b3, e3}, {a3, b4, e4}}}
Flatten
ing once will give you what you want:
Flatten[MapThread[Append, {Outer[List, l1, l2], l3}, 2], 1]
{{a1, b1, c1}, {a1, b2, c2}, {a1, b3, c3}, {a1, b4, c4}, {a2, b1, d1}, {a2, b2, d2}, {a2, b3, d3}, {a2, b4, d4}, {a3, b1, e1}, {a3, b2, e2}, {a3, b3, e3}, {a3, b4, e4}}
If you're not interested in the nested list above, then you can get straight to the result with
MapThread[Append, {Tuples[{l1, l2}], Flatten[l3]}]
{{a1, b1, c1}, {a1, b2, c2}, {a1, b3, c3}, {a1, b4, c4}, {a2, b1, d1}, {a2, b2, d2}, {a2, b3, d3}, {a2, b4, d4}, {a3, b1, e1}, {a3, b2, e2}, {a3, b3, e3}, {a3, b4, e4}}
(in effect, Flatten
ing before MapThread
ing).
edited 24 mins ago
answered 42 mins ago
RomanRoman
1,626614
1,626614
add a comment |
add a comment |
Brian is a new contributor. Be nice, and check out our Code of Conduct.
Brian is a new contributor. Be nice, and check out our Code of Conduct.
Brian is a new contributor. Be nice, and check out our Code of Conduct.
Brian is a new contributor. Be nice, and check out our Code of Conduct.
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%2f192020%2fhow-to-build-a-list-from-separate-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