Truncate a set of lists until a subset of entries matches?











up vote
3
down vote

favorite












Consider lists of vectors of elements like



list[1]={{a1,b1,c11},{a2,b2,c12},{a3,b3,c13},{a4,b4,c14},{a5,b5,c15}};
list[2]={{a1,b1,c21} ,{a3,b3,c23},{a4,b4,c24},{a5,b5,c25}};
list[3]={{a1,b1,c31},{a2,b2,c32},{a3,b3,c33} ,{a5,b5,c35}};


where the length 3 of vectors in lists is just an example and could be longer, and there may be more than 3 such lists.



I would like to have a function truncate[listoflists_] that takes all these lists and truncates them to only have the vectors containing ai,bi entries that are present in all of them:



{list[1],list[2],list[3]}=truncate[{list[1],list[2],list[3]}];
list[1]
list[2]
list[3]



{{a1,b1,c11},{a3,b3,c13},{a5,b5,c15}}



{{a1,b1,c21},{a3,b3,c23},{a5,b5,c25}}



{{a1,b1,c31},{a3,b3,c33},{a5,b5,c35}}




Is there a quick way to do this in Mathematica? Thanks for any suggestion.










share|improve this question


























    up vote
    3
    down vote

    favorite












    Consider lists of vectors of elements like



    list[1]={{a1,b1,c11},{a2,b2,c12},{a3,b3,c13},{a4,b4,c14},{a5,b5,c15}};
    list[2]={{a1,b1,c21} ,{a3,b3,c23},{a4,b4,c24},{a5,b5,c25}};
    list[3]={{a1,b1,c31},{a2,b2,c32},{a3,b3,c33} ,{a5,b5,c35}};


    where the length 3 of vectors in lists is just an example and could be longer, and there may be more than 3 such lists.



    I would like to have a function truncate[listoflists_] that takes all these lists and truncates them to only have the vectors containing ai,bi entries that are present in all of them:



    {list[1],list[2],list[3]}=truncate[{list[1],list[2],list[3]}];
    list[1]
    list[2]
    list[3]



    {{a1,b1,c11},{a3,b3,c13},{a5,b5,c15}}



    {{a1,b1,c21},{a3,b3,c23},{a5,b5,c25}}



    {{a1,b1,c31},{a3,b3,c33},{a5,b5,c35}}




    Is there a quick way to do this in Mathematica? Thanks for any suggestion.










    share|improve this question
























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      Consider lists of vectors of elements like



      list[1]={{a1,b1,c11},{a2,b2,c12},{a3,b3,c13},{a4,b4,c14},{a5,b5,c15}};
      list[2]={{a1,b1,c21} ,{a3,b3,c23},{a4,b4,c24},{a5,b5,c25}};
      list[3]={{a1,b1,c31},{a2,b2,c32},{a3,b3,c33} ,{a5,b5,c35}};


      where the length 3 of vectors in lists is just an example and could be longer, and there may be more than 3 such lists.



      I would like to have a function truncate[listoflists_] that takes all these lists and truncates them to only have the vectors containing ai,bi entries that are present in all of them:



      {list[1],list[2],list[3]}=truncate[{list[1],list[2],list[3]}];
      list[1]
      list[2]
      list[3]



      {{a1,b1,c11},{a3,b3,c13},{a5,b5,c15}}



      {{a1,b1,c21},{a3,b3,c23},{a5,b5,c25}}



      {{a1,b1,c31},{a3,b3,c33},{a5,b5,c35}}




      Is there a quick way to do this in Mathematica? Thanks for any suggestion.










      share|improve this question













      Consider lists of vectors of elements like



      list[1]={{a1,b1,c11},{a2,b2,c12},{a3,b3,c13},{a4,b4,c14},{a5,b5,c15}};
      list[2]={{a1,b1,c21} ,{a3,b3,c23},{a4,b4,c24},{a5,b5,c25}};
      list[3]={{a1,b1,c31},{a2,b2,c32},{a3,b3,c33} ,{a5,b5,c35}};


      where the length 3 of vectors in lists is just an example and could be longer, and there may be more than 3 such lists.



      I would like to have a function truncate[listoflists_] that takes all these lists and truncates them to only have the vectors containing ai,bi entries that are present in all of them:



      {list[1],list[2],list[3]}=truncate[{list[1],list[2],list[3]}];
      list[1]
      list[2]
      list[3]



      {{a1,b1,c11},{a3,b3,c13},{a5,b5,c15}}



      {{a1,b1,c21},{a3,b3,c23},{a5,b5,c25}}



      {{a1,b1,c31},{a3,b3,c33},{a5,b5,c35}}




      Is there a quick way to do this in Mathematica? Thanks for any suggestion.







      list-manipulation function-construction






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 at 21:04









      Kagaratsch

      4,57731246




      4,57731246






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          It is short (does not mean quick) to get it via associations:



          truncate = Values @ KeyIntersection @ Map[#[[;; 2]] -> # &, #, {2}] &


          We convert {a1,b1,c11} to {a1,b1}->{a1,b1,c11} and everything else is straightforward.






          share|improve this answer



















          • 1




            Love it when a mod ends up in the LQ review. :)
            – Michael E2
            Nov 23 at 21:45










          • @MichaelE2 ok, explanation added.
            – Kuba
            Nov 23 at 21:53










          • I approved it anyway. It's not always clear that excessive exposition would be an improvement.
            – Michael E2
            Nov 23 at 22:00










          • I have changed it to truncate = Values@KeyIntersection@Map[{#[[1]], #[[2]]} -> # &, #, {2}] & to make it compatible with longer vector cases.
            – Kagaratsch
            Nov 23 at 23:35






          • 1




            @Kagaratsch right, you mentioned it. I edited the code.
            – Kuba
            Nov 24 at 0:27


















          up vote
          1
          down vote













          Also



          tF = Module[{i = Intersection@@#[[;;, ;;, #2]], k = #2}, Select[MemberQ[i, #[[k]]]&]/@ #]&

          lists = list /@ {1, 2, 3};
          tF[lists, {1, 2}]



          {{{a1, b1, c11}, {a3, b3, c13}, {a5, b5, c15}},

          {{a1, b1, c21}, {a3, b3, c23}, {a5, b5, c25}},

          {{a1, b1, c31}, {a3, b3, c33}, {a5, b5, c35}}}







          share|improve this answer





















            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',
            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
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f186600%2ftruncate-a-set-of-lists-until-a-subset-of-entries-matches%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








            up vote
            3
            down vote



            accepted










            It is short (does not mean quick) to get it via associations:



            truncate = Values @ KeyIntersection @ Map[#[[;; 2]] -> # &, #, {2}] &


            We convert {a1,b1,c11} to {a1,b1}->{a1,b1,c11} and everything else is straightforward.






            share|improve this answer



















            • 1




              Love it when a mod ends up in the LQ review. :)
              – Michael E2
              Nov 23 at 21:45










            • @MichaelE2 ok, explanation added.
              – Kuba
              Nov 23 at 21:53










            • I approved it anyway. It's not always clear that excessive exposition would be an improvement.
              – Michael E2
              Nov 23 at 22:00










            • I have changed it to truncate = Values@KeyIntersection@Map[{#[[1]], #[[2]]} -> # &, #, {2}] & to make it compatible with longer vector cases.
              – Kagaratsch
              Nov 23 at 23:35






            • 1




              @Kagaratsch right, you mentioned it. I edited the code.
              – Kuba
              Nov 24 at 0:27















            up vote
            3
            down vote



            accepted










            It is short (does not mean quick) to get it via associations:



            truncate = Values @ KeyIntersection @ Map[#[[;; 2]] -> # &, #, {2}] &


            We convert {a1,b1,c11} to {a1,b1}->{a1,b1,c11} and everything else is straightforward.






            share|improve this answer



















            • 1




              Love it when a mod ends up in the LQ review. :)
              – Michael E2
              Nov 23 at 21:45










            • @MichaelE2 ok, explanation added.
              – Kuba
              Nov 23 at 21:53










            • I approved it anyway. It's not always clear that excessive exposition would be an improvement.
              – Michael E2
              Nov 23 at 22:00










            • I have changed it to truncate = Values@KeyIntersection@Map[{#[[1]], #[[2]]} -> # &, #, {2}] & to make it compatible with longer vector cases.
              – Kagaratsch
              Nov 23 at 23:35






            • 1




              @Kagaratsch right, you mentioned it. I edited the code.
              – Kuba
              Nov 24 at 0:27













            up vote
            3
            down vote



            accepted







            up vote
            3
            down vote



            accepted






            It is short (does not mean quick) to get it via associations:



            truncate = Values @ KeyIntersection @ Map[#[[;; 2]] -> # &, #, {2}] &


            We convert {a1,b1,c11} to {a1,b1}->{a1,b1,c11} and everything else is straightforward.






            share|improve this answer














            It is short (does not mean quick) to get it via associations:



            truncate = Values @ KeyIntersection @ Map[#[[;; 2]] -> # &, #, {2}] &


            We convert {a1,b1,c11} to {a1,b1}->{a1,b1,c11} and everything else is straightforward.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 24 at 0:27

























            answered Nov 23 at 21:23









            Kuba

            102k12200511




            102k12200511








            • 1




              Love it when a mod ends up in the LQ review. :)
              – Michael E2
              Nov 23 at 21:45










            • @MichaelE2 ok, explanation added.
              – Kuba
              Nov 23 at 21:53










            • I approved it anyway. It's not always clear that excessive exposition would be an improvement.
              – Michael E2
              Nov 23 at 22:00










            • I have changed it to truncate = Values@KeyIntersection@Map[{#[[1]], #[[2]]} -> # &, #, {2}] & to make it compatible with longer vector cases.
              – Kagaratsch
              Nov 23 at 23:35






            • 1




              @Kagaratsch right, you mentioned it. I edited the code.
              – Kuba
              Nov 24 at 0:27














            • 1




              Love it when a mod ends up in the LQ review. :)
              – Michael E2
              Nov 23 at 21:45










            • @MichaelE2 ok, explanation added.
              – Kuba
              Nov 23 at 21:53










            • I approved it anyway. It's not always clear that excessive exposition would be an improvement.
              – Michael E2
              Nov 23 at 22:00










            • I have changed it to truncate = Values@KeyIntersection@Map[{#[[1]], #[[2]]} -> # &, #, {2}] & to make it compatible with longer vector cases.
              – Kagaratsch
              Nov 23 at 23:35






            • 1




              @Kagaratsch right, you mentioned it. I edited the code.
              – Kuba
              Nov 24 at 0:27








            1




            1




            Love it when a mod ends up in the LQ review. :)
            – Michael E2
            Nov 23 at 21:45




            Love it when a mod ends up in the LQ review. :)
            – Michael E2
            Nov 23 at 21:45












            @MichaelE2 ok, explanation added.
            – Kuba
            Nov 23 at 21:53




            @MichaelE2 ok, explanation added.
            – Kuba
            Nov 23 at 21:53












            I approved it anyway. It's not always clear that excessive exposition would be an improvement.
            – Michael E2
            Nov 23 at 22:00




            I approved it anyway. It's not always clear that excessive exposition would be an improvement.
            – Michael E2
            Nov 23 at 22:00












            I have changed it to truncate = Values@KeyIntersection@Map[{#[[1]], #[[2]]} -> # &, #, {2}] & to make it compatible with longer vector cases.
            – Kagaratsch
            Nov 23 at 23:35




            I have changed it to truncate = Values@KeyIntersection@Map[{#[[1]], #[[2]]} -> # &, #, {2}] & to make it compatible with longer vector cases.
            – Kagaratsch
            Nov 23 at 23:35




            1




            1




            @Kagaratsch right, you mentioned it. I edited the code.
            – Kuba
            Nov 24 at 0:27




            @Kagaratsch right, you mentioned it. I edited the code.
            – Kuba
            Nov 24 at 0:27










            up vote
            1
            down vote













            Also



            tF = Module[{i = Intersection@@#[[;;, ;;, #2]], k = #2}, Select[MemberQ[i, #[[k]]]&]/@ #]&

            lists = list /@ {1, 2, 3};
            tF[lists, {1, 2}]



            {{{a1, b1, c11}, {a3, b3, c13}, {a5, b5, c15}},

            {{a1, b1, c21}, {a3, b3, c23}, {a5, b5, c25}},

            {{a1, b1, c31}, {a3, b3, c33}, {a5, b5, c35}}}







            share|improve this answer

























              up vote
              1
              down vote













              Also



              tF = Module[{i = Intersection@@#[[;;, ;;, #2]], k = #2}, Select[MemberQ[i, #[[k]]]&]/@ #]&

              lists = list /@ {1, 2, 3};
              tF[lists, {1, 2}]



              {{{a1, b1, c11}, {a3, b3, c13}, {a5, b5, c15}},

              {{a1, b1, c21}, {a3, b3, c23}, {a5, b5, c25}},

              {{a1, b1, c31}, {a3, b3, c33}, {a5, b5, c35}}}







              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                Also



                tF = Module[{i = Intersection@@#[[;;, ;;, #2]], k = #2}, Select[MemberQ[i, #[[k]]]&]/@ #]&

                lists = list /@ {1, 2, 3};
                tF[lists, {1, 2}]



                {{{a1, b1, c11}, {a3, b3, c13}, {a5, b5, c15}},

                {{a1, b1, c21}, {a3, b3, c23}, {a5, b5, c25}},

                {{a1, b1, c31}, {a3, b3, c33}, {a5, b5, c35}}}







                share|improve this answer












                Also



                tF = Module[{i = Intersection@@#[[;;, ;;, #2]], k = #2}, Select[MemberQ[i, #[[k]]]&]/@ #]&

                lists = list /@ {1, 2, 3};
                tF[lists, {1, 2}]



                {{{a1, b1, c11}, {a3, b3, c13}, {a5, b5, c15}},

                {{a1, b1, c21}, {a3, b3, c23}, {a5, b5, c25}},

                {{a1, b1, c31}, {a3, b3, c33}, {a5, b5, c35}}}








                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 24 at 19:32









                kglr

                173k8196401




                173k8196401






























                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f186600%2ftruncate-a-set-of-lists-until-a-subset-of-entries-matches%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 change which sound is reproduced for terminal bell?

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                    Can I use Tabulator js library in my java Spring + Thymeleaf project?