Reshaping nested list
$begingroup$
I have the feeling this is a very basic question, but I can't seem to find the way to solve this easily.
I have imported a set of data from a txt file into a table so the resulting list has the following structure:
{{x1,y1,f1},{x2,y2,f2},...}
I want it to reshape it into this form:
{{{x1,y1},f1},{{x2,y2},f2},...}
I apologize because I know it must be trivial but I haven't found the way yet. I've always had problems manipulating lists in Mathematica so, if anyone has any resource I can use to learn about this it'd be very much appreciated.
Regards
list-manipulation
New contributor
$endgroup$
add a comment |
$begingroup$
I have the feeling this is a very basic question, but I can't seem to find the way to solve this easily.
I have imported a set of data from a txt file into a table so the resulting list has the following structure:
{{x1,y1,f1},{x2,y2,f2},...}
I want it to reshape it into this form:
{{{x1,y1},f1},{{x2,y2},f2},...}
I apologize because I know it must be trivial but I haven't found the way yet. I've always had problems manipulating lists in Mathematica so, if anyone has any resource I can use to learn about this it'd be very much appreciated.
Regards
list-manipulation
New contributor
$endgroup$
1
$begingroup$
Look at the output forApply[{{#1, #2}, #3} &, {a, b, c}]
and it should help you. CheckApply
for more details.
$endgroup$
– Jason B.
5 hours ago
$begingroup$
Also possible is the following:reshape = Curry[Replace, {3, 1, 2}][{s_, t_, u_} :> {{s, t}, u}, {1}]
. Then{{x1, y1, f1}, {x2, y2, f2}} // reshape
gives the desired result.
$endgroup$
– Shredderroy
3 hours ago
$begingroup$
@JasonB. this should beApply[{{#1, #2}, #3} &, {{x1, y1, f1}, {x2, y2, f2}}, {1}]
$endgroup$
– GenericAccountName
2 hours ago
add a comment |
$begingroup$
I have the feeling this is a very basic question, but I can't seem to find the way to solve this easily.
I have imported a set of data from a txt file into a table so the resulting list has the following structure:
{{x1,y1,f1},{x2,y2,f2},...}
I want it to reshape it into this form:
{{{x1,y1},f1},{{x2,y2},f2},...}
I apologize because I know it must be trivial but I haven't found the way yet. I've always had problems manipulating lists in Mathematica so, if anyone has any resource I can use to learn about this it'd be very much appreciated.
Regards
list-manipulation
New contributor
$endgroup$
I have the feeling this is a very basic question, but I can't seem to find the way to solve this easily.
I have imported a set of data from a txt file into a table so the resulting list has the following structure:
{{x1,y1,f1},{x2,y2,f2},...}
I want it to reshape it into this form:
{{{x1,y1},f1},{{x2,y2},f2},...}
I apologize because I know it must be trivial but I haven't found the way yet. I've always had problems manipulating lists in Mathematica so, if anyone has any resource I can use to learn about this it'd be very much appreciated.
Regards
list-manipulation
list-manipulation
New contributor
New contributor
New contributor
asked 5 hours ago
Mikel GarcíaMikel García
161
161
New contributor
New contributor
1
$begingroup$
Look at the output forApply[{{#1, #2}, #3} &, {a, b, c}]
and it should help you. CheckApply
for more details.
$endgroup$
– Jason B.
5 hours ago
$begingroup$
Also possible is the following:reshape = Curry[Replace, {3, 1, 2}][{s_, t_, u_} :> {{s, t}, u}, {1}]
. Then{{x1, y1, f1}, {x2, y2, f2}} // reshape
gives the desired result.
$endgroup$
– Shredderroy
3 hours ago
$begingroup$
@JasonB. this should beApply[{{#1, #2}, #3} &, {{x1, y1, f1}, {x2, y2, f2}}, {1}]
$endgroup$
– GenericAccountName
2 hours ago
add a comment |
1
$begingroup$
Look at the output forApply[{{#1, #2}, #3} &, {a, b, c}]
and it should help you. CheckApply
for more details.
$endgroup$
– Jason B.
5 hours ago
$begingroup$
Also possible is the following:reshape = Curry[Replace, {3, 1, 2}][{s_, t_, u_} :> {{s, t}, u}, {1}]
. Then{{x1, y1, f1}, {x2, y2, f2}} // reshape
gives the desired result.
$endgroup$
– Shredderroy
3 hours ago
$begingroup$
@JasonB. this should beApply[{{#1, #2}, #3} &, {{x1, y1, f1}, {x2, y2, f2}}, {1}]
$endgroup$
– GenericAccountName
2 hours ago
1
1
$begingroup$
Look at the output for
Apply[{{#1, #2}, #3} &, {a, b, c}]
and it should help you. Check Apply
for more details.$endgroup$
– Jason B.
5 hours ago
$begingroup$
Look at the output for
Apply[{{#1, #2}, #3} &, {a, b, c}]
and it should help you. Check Apply
for more details.$endgroup$
– Jason B.
5 hours ago
$begingroup$
Also possible is the following:
reshape = Curry[Replace, {3, 1, 2}][{s_, t_, u_} :> {{s, t}, u}, {1}]
. Then {{x1, y1, f1}, {x2, y2, f2}} // reshape
gives the desired result.$endgroup$
– Shredderroy
3 hours ago
$begingroup$
Also possible is the following:
reshape = Curry[Replace, {3, 1, 2}][{s_, t_, u_} :> {{s, t}, u}, {1}]
. Then {{x1, y1, f1}, {x2, y2, f2}} // reshape
gives the desired result.$endgroup$
– Shredderroy
3 hours ago
$begingroup$
@JasonB. this should be
Apply[{{#1, #2}, #3} &, {{x1, y1, f1}, {x2, y2, f2}}, {1}]
$endgroup$
– GenericAccountName
2 hours ago
$begingroup$
@JasonB. this should be
Apply[{{#1, #2}, #3} &, {{x1, y1, f1}, {x2, y2, f2}}, {1}]
$endgroup$
– GenericAccountName
2 hours ago
add a comment |
4 Answers
4
active
oldest
votes
$begingroup$
list = {{x1, y1, f1}, {x2, y2, f2}};
Transpose[{Drop[list, 0, -1], list[[All, -1]]}]
(* {{{x1, y1}, f1}, {{x2, y2}, f2}} *)
New contributor
$endgroup$
add a comment |
$begingroup$
list = {{x1, y1, f1}, {x2, y2, f2}};
{Most[#], Last[#]} & /@ list
(* {{{x1, y1}, f1}, {{x2, y2}, f2}} *)
$endgroup$
add a comment |
$begingroup$
I think my vote is this so far, but I bet there's even cleaner/more clever ways to do this:
data = {{x1, y1, f1}, {x2, y2, f2}}
Replace[data, {x_, y_, f_} :> {{x, y}, f}, {1}]
I like the Apply solution just fine too:
Apply[{{#1, #2}, #3} &, {{x1, y1, f1}, {x2, y2, f2}}, {1}]
For larger data they perform similarly, with Replace winning slightly:
In[53]:= data=(ToExpression/@{"x"<>#,"y"<>#,"f"<>#})&/@(ToString[#]&/@Range[1000000]);
Apply[{{#1,#2},#3}&,data,{1}]//RepeatedTiming//First
Replace[data,{x_,y_,f_}:>{{x,y},f},{1}]//RepeatedTiming//First
Out[54]= 0.825
Out[55]= 0.740
This is much better than the most naive approach:
In[57]:= ({{#[[1]], #[[2]]}, #[[3]]} & /@ data) //
RepeatedTiming // First
Out[57]= 1.859
The solution from @Oppenede is similar in timing to Apply
In[77]:= Transpose[{Drop[data, 0, -1], data[[All, -1]]}] // RepeatedTiming // First
Out[77]= 0.836
$endgroup$
add a comment |
$begingroup$
Use a rule and replacement:
{{x1, y1, f1}, {x2, y2, f2}} /. {x_, y_, f_} -> {{x, y}, f}
(* {{{x1,y1},f1},{{x2,y2},f2}} *)
$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
});
}
});
Mikel García 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%2f190877%2freshaping-nested-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
list = {{x1, y1, f1}, {x2, y2, f2}};
Transpose[{Drop[list, 0, -1], list[[All, -1]]}]
(* {{{x1, y1}, f1}, {{x2, y2}, f2}} *)
New contributor
$endgroup$
add a comment |
$begingroup$
list = {{x1, y1, f1}, {x2, y2, f2}};
Transpose[{Drop[list, 0, -1], list[[All, -1]]}]
(* {{{x1, y1}, f1}, {{x2, y2}, f2}} *)
New contributor
$endgroup$
add a comment |
$begingroup$
list = {{x1, y1, f1}, {x2, y2, f2}};
Transpose[{Drop[list, 0, -1], list[[All, -1]]}]
(* {{{x1, y1}, f1}, {{x2, y2}, f2}} *)
New contributor
$endgroup$
list = {{x1, y1, f1}, {x2, y2, f2}};
Transpose[{Drop[list, 0, -1], list[[All, -1]]}]
(* {{{x1, y1}, f1}, {{x2, y2}, f2}} *)
New contributor
New contributor
answered 4 hours ago
OppenedeOppenede
1211
1211
New contributor
New contributor
add a comment |
add a comment |
$begingroup$
list = {{x1, y1, f1}, {x2, y2, f2}};
{Most[#], Last[#]} & /@ list
(* {{{x1, y1}, f1}, {{x2, y2}, f2}} *)
$endgroup$
add a comment |
$begingroup$
list = {{x1, y1, f1}, {x2, y2, f2}};
{Most[#], Last[#]} & /@ list
(* {{{x1, y1}, f1}, {{x2, y2}, f2}} *)
$endgroup$
add a comment |
$begingroup$
list = {{x1, y1, f1}, {x2, y2, f2}};
{Most[#], Last[#]} & /@ list
(* {{{x1, y1}, f1}, {{x2, y2}, f2}} *)
$endgroup$
list = {{x1, y1, f1}, {x2, y2, f2}};
{Most[#], Last[#]} & /@ list
(* {{{x1, y1}, f1}, {{x2, y2}, f2}} *)
answered 2 hours ago
Rohit NamjoshiRohit Namjoshi
8351212
8351212
add a comment |
add a comment |
$begingroup$
I think my vote is this so far, but I bet there's even cleaner/more clever ways to do this:
data = {{x1, y1, f1}, {x2, y2, f2}}
Replace[data, {x_, y_, f_} :> {{x, y}, f}, {1}]
I like the Apply solution just fine too:
Apply[{{#1, #2}, #3} &, {{x1, y1, f1}, {x2, y2, f2}}, {1}]
For larger data they perform similarly, with Replace winning slightly:
In[53]:= data=(ToExpression/@{"x"<>#,"y"<>#,"f"<>#})&/@(ToString[#]&/@Range[1000000]);
Apply[{{#1,#2},#3}&,data,{1}]//RepeatedTiming//First
Replace[data,{x_,y_,f_}:>{{x,y},f},{1}]//RepeatedTiming//First
Out[54]= 0.825
Out[55]= 0.740
This is much better than the most naive approach:
In[57]:= ({{#[[1]], #[[2]]}, #[[3]]} & /@ data) //
RepeatedTiming // First
Out[57]= 1.859
The solution from @Oppenede is similar in timing to Apply
In[77]:= Transpose[{Drop[data, 0, -1], data[[All, -1]]}] // RepeatedTiming // First
Out[77]= 0.836
$endgroup$
add a comment |
$begingroup$
I think my vote is this so far, but I bet there's even cleaner/more clever ways to do this:
data = {{x1, y1, f1}, {x2, y2, f2}}
Replace[data, {x_, y_, f_} :> {{x, y}, f}, {1}]
I like the Apply solution just fine too:
Apply[{{#1, #2}, #3} &, {{x1, y1, f1}, {x2, y2, f2}}, {1}]
For larger data they perform similarly, with Replace winning slightly:
In[53]:= data=(ToExpression/@{"x"<>#,"y"<>#,"f"<>#})&/@(ToString[#]&/@Range[1000000]);
Apply[{{#1,#2},#3}&,data,{1}]//RepeatedTiming//First
Replace[data,{x_,y_,f_}:>{{x,y},f},{1}]//RepeatedTiming//First
Out[54]= 0.825
Out[55]= 0.740
This is much better than the most naive approach:
In[57]:= ({{#[[1]], #[[2]]}, #[[3]]} & /@ data) //
RepeatedTiming // First
Out[57]= 1.859
The solution from @Oppenede is similar in timing to Apply
In[77]:= Transpose[{Drop[data, 0, -1], data[[All, -1]]}] // RepeatedTiming // First
Out[77]= 0.836
$endgroup$
add a comment |
$begingroup$
I think my vote is this so far, but I bet there's even cleaner/more clever ways to do this:
data = {{x1, y1, f1}, {x2, y2, f2}}
Replace[data, {x_, y_, f_} :> {{x, y}, f}, {1}]
I like the Apply solution just fine too:
Apply[{{#1, #2}, #3} &, {{x1, y1, f1}, {x2, y2, f2}}, {1}]
For larger data they perform similarly, with Replace winning slightly:
In[53]:= data=(ToExpression/@{"x"<>#,"y"<>#,"f"<>#})&/@(ToString[#]&/@Range[1000000]);
Apply[{{#1,#2},#3}&,data,{1}]//RepeatedTiming//First
Replace[data,{x_,y_,f_}:>{{x,y},f},{1}]//RepeatedTiming//First
Out[54]= 0.825
Out[55]= 0.740
This is much better than the most naive approach:
In[57]:= ({{#[[1]], #[[2]]}, #[[3]]} & /@ data) //
RepeatedTiming // First
Out[57]= 1.859
The solution from @Oppenede is similar in timing to Apply
In[77]:= Transpose[{Drop[data, 0, -1], data[[All, -1]]}] // RepeatedTiming // First
Out[77]= 0.836
$endgroup$
I think my vote is this so far, but I bet there's even cleaner/more clever ways to do this:
data = {{x1, y1, f1}, {x2, y2, f2}}
Replace[data, {x_, y_, f_} :> {{x, y}, f}, {1}]
I like the Apply solution just fine too:
Apply[{{#1, #2}, #3} &, {{x1, y1, f1}, {x2, y2, f2}}, {1}]
For larger data they perform similarly, with Replace winning slightly:
In[53]:= data=(ToExpression/@{"x"<>#,"y"<>#,"f"<>#})&/@(ToString[#]&/@Range[1000000]);
Apply[{{#1,#2},#3}&,data,{1}]//RepeatedTiming//First
Replace[data,{x_,y_,f_}:>{{x,y},f},{1}]//RepeatedTiming//First
Out[54]= 0.825
Out[55]= 0.740
This is much better than the most naive approach:
In[57]:= ({{#[[1]], #[[2]]}, #[[3]]} & /@ data) //
RepeatedTiming // First
Out[57]= 1.859
The solution from @Oppenede is similar in timing to Apply
In[77]:= Transpose[{Drop[data, 0, -1], data[[All, -1]]}] // RepeatedTiming // First
Out[77]= 0.836
answered 2 hours ago
GenericAccountNameGenericAccountName
68227
68227
add a comment |
add a comment |
$begingroup$
Use a rule and replacement:
{{x1, y1, f1}, {x2, y2, f2}} /. {x_, y_, f_} -> {{x, y}, f}
(* {{{x1,y1},f1},{{x2,y2},f2}} *)
$endgroup$
add a comment |
$begingroup$
Use a rule and replacement:
{{x1, y1, f1}, {x2, y2, f2}} /. {x_, y_, f_} -> {{x, y}, f}
(* {{{x1,y1},f1},{{x2,y2},f2}} *)
$endgroup$
add a comment |
$begingroup$
Use a rule and replacement:
{{x1, y1, f1}, {x2, y2, f2}} /. {x_, y_, f_} -> {{x, y}, f}
(* {{{x1,y1},f1},{{x2,y2},f2}} *)
$endgroup$
Use a rule and replacement:
{{x1, y1, f1}, {x2, y2, f2}} /. {x_, y_, f_} -> {{x, y}, f}
(* {{{x1,y1},f1},{{x2,y2},f2}} *)
answered 2 hours ago
David KeithDavid Keith
1,151313
1,151313
add a comment |
add a comment |
Mikel García is a new contributor. Be nice, and check out our Code of Conduct.
Mikel García is a new contributor. Be nice, and check out our Code of Conduct.
Mikel García is a new contributor. Be nice, and check out our Code of Conduct.
Mikel García 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%2f190877%2freshaping-nested-list%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
1
$begingroup$
Look at the output for
Apply[{{#1, #2}, #3} &, {a, b, c}]
and it should help you. CheckApply
for more details.$endgroup$
– Jason B.
5 hours ago
$begingroup$
Also possible is the following:
reshape = Curry[Replace, {3, 1, 2}][{s_, t_, u_} :> {{s, t}, u}, {1}]
. Then{{x1, y1, f1}, {x2, y2, f2}} // reshape
gives the desired result.$endgroup$
– Shredderroy
3 hours ago
$begingroup$
@JasonB. this should be
Apply[{{#1, #2}, #3} &, {{x1, y1, f1}, {x2, y2, f2}}, {1}]
$endgroup$
– GenericAccountName
2 hours ago