Reshaping nested list












3












$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










share|improve this question







New contributor




Mikel García is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$








  • 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$
    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
















3












$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










share|improve this question







New contributor




Mikel García is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$








  • 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$
    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














3












3








3





$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










share|improve this question







New contributor




Mikel García is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$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






share|improve this question







New contributor




Mikel García is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Mikel García is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Mikel García is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 5 hours ago









Mikel GarcíaMikel García

161




161




New contributor




Mikel García is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Mikel García is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Mikel García is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 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$
    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














  • 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$
    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








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










4 Answers
4






active

oldest

votes


















2












$begingroup$

list = {{x1, y1, f1}, {x2, y2, f2}};

Transpose[{Drop[list, 0, -1], list[[All, -1]]}]
(* {{{x1, y1}, f1}, {{x2, y2}, f2}} *)





share|improve this answer








New contributor




Oppenede is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$





















    2












    $begingroup$

    list = {{x1, y1, f1}, {x2, y2, f2}};

    {Most[#], Last[#]} & /@ list

    (* {{{x1, y1}, f1}, {{x2, y2}, f2}} *)





    share|improve this answer









    $endgroup$





















      1












      $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





      share|improve this answer









      $endgroup$





















        1












        $begingroup$

        Use a rule and replacement:



        {{x1, y1, f1}, {x2, y2, f2}} /. {x_, y_, f_} -> {{x, y}, f}

        (* {{{x1,y1},f1},{{x2,y2},f2}} *)





        share|improve this answer









        $endgroup$













          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.










          draft saved

          draft discarded


















          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









          2












          $begingroup$

          list = {{x1, y1, f1}, {x2, y2, f2}};

          Transpose[{Drop[list, 0, -1], list[[All, -1]]}]
          (* {{{x1, y1}, f1}, {{x2, y2}, f2}} *)





          share|improve this answer








          New contributor




          Oppenede is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          $endgroup$


















            2












            $begingroup$

            list = {{x1, y1, f1}, {x2, y2, f2}};

            Transpose[{Drop[list, 0, -1], list[[All, -1]]}]
            (* {{{x1, y1}, f1}, {{x2, y2}, f2}} *)





            share|improve this answer








            New contributor




            Oppenede is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            $endgroup$
















              2












              2








              2





              $begingroup$

              list = {{x1, y1, f1}, {x2, y2, f2}};

              Transpose[{Drop[list, 0, -1], list[[All, -1]]}]
              (* {{{x1, y1}, f1}, {{x2, y2}, f2}} *)





              share|improve this answer








              New contributor




              Oppenede is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.






              $endgroup$



              list = {{x1, y1, f1}, {x2, y2, f2}};

              Transpose[{Drop[list, 0, -1], list[[All, -1]]}]
              (* {{{x1, y1}, f1}, {{x2, y2}, f2}} *)






              share|improve this answer








              New contributor




              Oppenede is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.









              share|improve this answer



              share|improve this answer






              New contributor




              Oppenede is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.









              answered 4 hours ago









              OppenedeOppenede

              1211




              1211




              New contributor




              Oppenede is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.





              New contributor





              Oppenede is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.






              Oppenede is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.























                  2












                  $begingroup$

                  list = {{x1, y1, f1}, {x2, y2, f2}};

                  {Most[#], Last[#]} & /@ list

                  (* {{{x1, y1}, f1}, {{x2, y2}, f2}} *)





                  share|improve this answer









                  $endgroup$


















                    2












                    $begingroup$

                    list = {{x1, y1, f1}, {x2, y2, f2}};

                    {Most[#], Last[#]} & /@ list

                    (* {{{x1, y1}, f1}, {{x2, y2}, f2}} *)





                    share|improve this answer









                    $endgroup$
















                      2












                      2








                      2





                      $begingroup$

                      list = {{x1, y1, f1}, {x2, y2, f2}};

                      {Most[#], Last[#]} & /@ list

                      (* {{{x1, y1}, f1}, {{x2, y2}, f2}} *)





                      share|improve this answer









                      $endgroup$



                      list = {{x1, y1, f1}, {x2, y2, f2}};

                      {Most[#], Last[#]} & /@ list

                      (* {{{x1, y1}, f1}, {{x2, y2}, f2}} *)






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 2 hours ago









                      Rohit NamjoshiRohit Namjoshi

                      8351212




                      8351212























                          1












                          $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





                          share|improve this answer









                          $endgroup$


















                            1












                            $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





                            share|improve this answer









                            $endgroup$
















                              1












                              1








                              1





                              $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





                              share|improve this answer









                              $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






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 2 hours ago









                              GenericAccountNameGenericAccountName

                              68227




                              68227























                                  1












                                  $begingroup$

                                  Use a rule and replacement:



                                  {{x1, y1, f1}, {x2, y2, f2}} /. {x_, y_, f_} -> {{x, y}, f}

                                  (* {{{x1,y1},f1},{{x2,y2},f2}} *)





                                  share|improve this answer









                                  $endgroup$


















                                    1












                                    $begingroup$

                                    Use a rule and replacement:



                                    {{x1, y1, f1}, {x2, y2, f2}} /. {x_, y_, f_} -> {{x, y}, f}

                                    (* {{{x1,y1},f1},{{x2,y2},f2}} *)





                                    share|improve this answer









                                    $endgroup$
















                                      1












                                      1








                                      1





                                      $begingroup$

                                      Use a rule and replacement:



                                      {{x1, y1, f1}, {x2, y2, f2}} /. {x_, y_, f_} -> {{x, y}, f}

                                      (* {{{x1,y1},f1},{{x2,y2},f2}} *)





                                      share|improve this answer









                                      $endgroup$



                                      Use a rule and replacement:



                                      {{x1, y1, f1}, {x2, y2, f2}} /. {x_, y_, f_} -> {{x, y}, f}

                                      (* {{{x1,y1},f1},{{x2,y2},f2}} *)






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered 2 hours ago









                                      David KeithDavid Keith

                                      1,151313




                                      1,151313






















                                          Mikel García is a new contributor. Be nice, and check out our Code of Conduct.










                                          draft saved

                                          draft discarded


















                                          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.




                                          draft saved


                                          draft discarded














                                          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





















































                                          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







                                          Popular posts from this blog

                                          How to make a Squid Proxy server?

                                          Is this a new Fibonacci Identity?

                                          19世紀