Selecting elements satisfying a condition












4















I have a list of lists(of lists):



{{{1}, {2}, {1, 2, 3}}, {{1}, {3}, {1, 2, 3}}, {{1}, {1, 2}, {1,2, 
3}}, {{1}, {2, 3}, {1, 2, 3}}, {{2}, {3}, {1, 2, 3}}, {{2}, {1,
2}, {1, 2, 3}}, {{2}, {2, 3}, {1, 2, 3}}, {{3}, {1, 2}, {1, 2,
3}}, {{3}, {2, 3}, {1, 2, 3}}, {{1, 2}, {2, 3}, {1, 2, 3}}}


From the list above I want to select those lists in which two consecutive elements share either a first element or last element. It also counts if the first element of the list shares the first entry with the second element of that list, but the second element shares the last entry with the third element, etc. So from the list above I would get:



{{1},{1,2},{1,2,3}},{{2},{1,2},{1,2,3}},{{2},{2,3},{1,2,3}},{{3},{2,3},{1,2,3}}


So I did the following:test4 = Table[Select[test3,First[#[[i+1]]]==First[#[[i]]] [Or] Last[#[[i+1]]]==Last[#[[i]]] &],{i,n-1}]



where test3 is my list of lists from above and n is the maximum length that the element in one of the list of lists can have, so in our case it is 3 and that number is known in advance. Unforunately the above doesn't work. It spits out more elements then there are in the initial list. I think it picks the elements which satify that condition for at least one pair of consecutive elements, which is not what I'm after. I'd really appreciate some help with this. Thanks!










share|improve this question





























    4















    I have a list of lists(of lists):



    {{{1}, {2}, {1, 2, 3}}, {{1}, {3}, {1, 2, 3}}, {{1}, {1, 2}, {1,2, 
    3}}, {{1}, {2, 3}, {1, 2, 3}}, {{2}, {3}, {1, 2, 3}}, {{2}, {1,
    2}, {1, 2, 3}}, {{2}, {2, 3}, {1, 2, 3}}, {{3}, {1, 2}, {1, 2,
    3}}, {{3}, {2, 3}, {1, 2, 3}}, {{1, 2}, {2, 3}, {1, 2, 3}}}


    From the list above I want to select those lists in which two consecutive elements share either a first element or last element. It also counts if the first element of the list shares the first entry with the second element of that list, but the second element shares the last entry with the third element, etc. So from the list above I would get:



    {{1},{1,2},{1,2,3}},{{2},{1,2},{1,2,3}},{{2},{2,3},{1,2,3}},{{3},{2,3},{1,2,3}}


    So I did the following:test4 = Table[Select[test3,First[#[[i+1]]]==First[#[[i]]] [Or] Last[#[[i+1]]]==Last[#[[i]]] &],{i,n-1}]



    where test3 is my list of lists from above and n is the maximum length that the element in one of the list of lists can have, so in our case it is 3 and that number is known in advance. Unforunately the above doesn't work. It spits out more elements then there are in the initial list. I think it picks the elements which satify that condition for at least one pair of consecutive elements, which is not what I'm after. I'd really appreciate some help with this. Thanks!










    share|improve this question



























      4












      4








      4


      2






      I have a list of lists(of lists):



      {{{1}, {2}, {1, 2, 3}}, {{1}, {3}, {1, 2, 3}}, {{1}, {1, 2}, {1,2, 
      3}}, {{1}, {2, 3}, {1, 2, 3}}, {{2}, {3}, {1, 2, 3}}, {{2}, {1,
      2}, {1, 2, 3}}, {{2}, {2, 3}, {1, 2, 3}}, {{3}, {1, 2}, {1, 2,
      3}}, {{3}, {2, 3}, {1, 2, 3}}, {{1, 2}, {2, 3}, {1, 2, 3}}}


      From the list above I want to select those lists in which two consecutive elements share either a first element or last element. It also counts if the first element of the list shares the first entry with the second element of that list, but the second element shares the last entry with the third element, etc. So from the list above I would get:



      {{1},{1,2},{1,2,3}},{{2},{1,2},{1,2,3}},{{2},{2,3},{1,2,3}},{{3},{2,3},{1,2,3}}


      So I did the following:test4 = Table[Select[test3,First[#[[i+1]]]==First[#[[i]]] [Or] Last[#[[i+1]]]==Last[#[[i]]] &],{i,n-1}]



      where test3 is my list of lists from above and n is the maximum length that the element in one of the list of lists can have, so in our case it is 3 and that number is known in advance. Unforunately the above doesn't work. It spits out more elements then there are in the initial list. I think it picks the elements which satify that condition for at least one pair of consecutive elements, which is not what I'm after. I'd really appreciate some help with this. Thanks!










      share|improve this question
















      I have a list of lists(of lists):



      {{{1}, {2}, {1, 2, 3}}, {{1}, {3}, {1, 2, 3}}, {{1}, {1, 2}, {1,2, 
      3}}, {{1}, {2, 3}, {1, 2, 3}}, {{2}, {3}, {1, 2, 3}}, {{2}, {1,
      2}, {1, 2, 3}}, {{2}, {2, 3}, {1, 2, 3}}, {{3}, {1, 2}, {1, 2,
      3}}, {{3}, {2, 3}, {1, 2, 3}}, {{1, 2}, {2, 3}, {1, 2, 3}}}


      From the list above I want to select those lists in which two consecutive elements share either a first element or last element. It also counts if the first element of the list shares the first entry with the second element of that list, but the second element shares the last entry with the third element, etc. So from the list above I would get:



      {{1},{1,2},{1,2,3}},{{2},{1,2},{1,2,3}},{{2},{2,3},{1,2,3}},{{3},{2,3},{1,2,3}}


      So I did the following:test4 = Table[Select[test3,First[#[[i+1]]]==First[#[[i]]] [Or] Last[#[[i+1]]]==Last[#[[i]]] &],{i,n-1}]



      where test3 is my list of lists from above and n is the maximum length that the element in one of the list of lists can have, so in our case it is 3 and that number is known in advance. Unforunately the above doesn't work. It spits out more elements then there are in the initial list. I think it picks the elements which satify that condition for at least one pair of consecutive elements, which is not what I'm after. I'd really appreciate some help with this. Thanks!







      list-manipulation filtering






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 21 hours ago









      Mr.Wizard

      230k294741040




      230k294741040










      asked yesterday









      amator2357amator2357

      353




      353






















          2 Answers
          2






          active

          oldest

          votes


















          6














          When I'm stuck with a problem like that, I always try to decompose it into simple, testable functions, that I can understand more or less at one glance.



          The first thing you want is to check if two lists share the first or last element:



          shareFirstOrLast[{a_, b_}] := 
          First[a] == First[b] || Last[a] == Last[b]


          Then you want to extend that to more than two elements:



          shareFirstOrLast[{a_, b_, rest__}] := 
          shareFirstOrLast[{a, b}] && shareFirstOrLast[{b, rest}]


          (this recursion will essentially call shareFirstOrLast[{a_, b_}] on every consecutive pair of elements.)



          Finally, test that function on different samples and edge cases. If it fits your needs, use it with select:



          Select[test3, shareFirstOrLast]



          {{{1}, {1, 2}, {1, 2, 3}}, {{2}, {1, 2}, {1, 2, 3}}, {{2}, {2, 3}, {1,
          2, 3}}, {{3}, {2, 3}, {1, 2, 3}}}







          share|improve this answer
























          • Brilliant, thank you so much! I really like your approach, I was looking at this problem from too wide of a perspective. I really appreciate it!

            – amator2357
            yesterday



















          1














          ClearAll[chainedQ]
          chainedQ = And @@ BlockMap[Or @@ Equal @@@ Transpose@#[[All, {1, -1}]] &, #, 2, 1] &;

          Select[list, chainedQ]



          {{{1}, {1, 2}, {1, 2, 3}}, {{2}, {1, 2}, {1, 2, 3}}, {{2}, {2, 3}, {1,
          2, 3}}, {{3}, {2, 3}, {1, 2, 3}}}




          Pick[list, chainedQ /@ list]



          {{{1}, {1, 2}, {1, 2, 3}}, {{2}, {1, 2}, {1, 2, 3}}, {{2}, {2, 3}, {1,
          2, 3}}, {{3}, {2, 3}, {1, 2, 3}}}







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


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f189367%2fselecting-elements-satisfying-a-condition%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









            6














            When I'm stuck with a problem like that, I always try to decompose it into simple, testable functions, that I can understand more or less at one glance.



            The first thing you want is to check if two lists share the first or last element:



            shareFirstOrLast[{a_, b_}] := 
            First[a] == First[b] || Last[a] == Last[b]


            Then you want to extend that to more than two elements:



            shareFirstOrLast[{a_, b_, rest__}] := 
            shareFirstOrLast[{a, b}] && shareFirstOrLast[{b, rest}]


            (this recursion will essentially call shareFirstOrLast[{a_, b_}] on every consecutive pair of elements.)



            Finally, test that function on different samples and edge cases. If it fits your needs, use it with select:



            Select[test3, shareFirstOrLast]



            {{{1}, {1, 2}, {1, 2, 3}}, {{2}, {1, 2}, {1, 2, 3}}, {{2}, {2, 3}, {1,
            2, 3}}, {{3}, {2, 3}, {1, 2, 3}}}







            share|improve this answer
























            • Brilliant, thank you so much! I really like your approach, I was looking at this problem from too wide of a perspective. I really appreciate it!

              – amator2357
              yesterday
















            6














            When I'm stuck with a problem like that, I always try to decompose it into simple, testable functions, that I can understand more or less at one glance.



            The first thing you want is to check if two lists share the first or last element:



            shareFirstOrLast[{a_, b_}] := 
            First[a] == First[b] || Last[a] == Last[b]


            Then you want to extend that to more than two elements:



            shareFirstOrLast[{a_, b_, rest__}] := 
            shareFirstOrLast[{a, b}] && shareFirstOrLast[{b, rest}]


            (this recursion will essentially call shareFirstOrLast[{a_, b_}] on every consecutive pair of elements.)



            Finally, test that function on different samples and edge cases. If it fits your needs, use it with select:



            Select[test3, shareFirstOrLast]



            {{{1}, {1, 2}, {1, 2, 3}}, {{2}, {1, 2}, {1, 2, 3}}, {{2}, {2, 3}, {1,
            2, 3}}, {{3}, {2, 3}, {1, 2, 3}}}







            share|improve this answer
























            • Brilliant, thank you so much! I really like your approach, I was looking at this problem from too wide of a perspective. I really appreciate it!

              – amator2357
              yesterday














            6












            6








            6







            When I'm stuck with a problem like that, I always try to decompose it into simple, testable functions, that I can understand more or less at one glance.



            The first thing you want is to check if two lists share the first or last element:



            shareFirstOrLast[{a_, b_}] := 
            First[a] == First[b] || Last[a] == Last[b]


            Then you want to extend that to more than two elements:



            shareFirstOrLast[{a_, b_, rest__}] := 
            shareFirstOrLast[{a, b}] && shareFirstOrLast[{b, rest}]


            (this recursion will essentially call shareFirstOrLast[{a_, b_}] on every consecutive pair of elements.)



            Finally, test that function on different samples and edge cases. If it fits your needs, use it with select:



            Select[test3, shareFirstOrLast]



            {{{1}, {1, 2}, {1, 2, 3}}, {{2}, {1, 2}, {1, 2, 3}}, {{2}, {2, 3}, {1,
            2, 3}}, {{3}, {2, 3}, {1, 2, 3}}}







            share|improve this answer













            When I'm stuck with a problem like that, I always try to decompose it into simple, testable functions, that I can understand more or less at one glance.



            The first thing you want is to check if two lists share the first or last element:



            shareFirstOrLast[{a_, b_}] := 
            First[a] == First[b] || Last[a] == Last[b]


            Then you want to extend that to more than two elements:



            shareFirstOrLast[{a_, b_, rest__}] := 
            shareFirstOrLast[{a, b}] && shareFirstOrLast[{b, rest}]


            (this recursion will essentially call shareFirstOrLast[{a_, b_}] on every consecutive pair of elements.)



            Finally, test that function on different samples and edge cases. If it fits your needs, use it with select:



            Select[test3, shareFirstOrLast]



            {{{1}, {1, 2}, {1, 2, 3}}, {{2}, {1, 2}, {1, 2, 3}}, {{2}, {2, 3}, {1,
            2, 3}}, {{3}, {2, 3}, {1, 2, 3}}}








            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered yesterday









            Niki EstnerNiki Estner

            30.9k374132




            30.9k374132













            • Brilliant, thank you so much! I really like your approach, I was looking at this problem from too wide of a perspective. I really appreciate it!

              – amator2357
              yesterday



















            • Brilliant, thank you so much! I really like your approach, I was looking at this problem from too wide of a perspective. I really appreciate it!

              – amator2357
              yesterday

















            Brilliant, thank you so much! I really like your approach, I was looking at this problem from too wide of a perspective. I really appreciate it!

            – amator2357
            yesterday





            Brilliant, thank you so much! I really like your approach, I was looking at this problem from too wide of a perspective. I really appreciate it!

            – amator2357
            yesterday











            1














            ClearAll[chainedQ]
            chainedQ = And @@ BlockMap[Or @@ Equal @@@ Transpose@#[[All, {1, -1}]] &, #, 2, 1] &;

            Select[list, chainedQ]



            {{{1}, {1, 2}, {1, 2, 3}}, {{2}, {1, 2}, {1, 2, 3}}, {{2}, {2, 3}, {1,
            2, 3}}, {{3}, {2, 3}, {1, 2, 3}}}




            Pick[list, chainedQ /@ list]



            {{{1}, {1, 2}, {1, 2, 3}}, {{2}, {1, 2}, {1, 2, 3}}, {{2}, {2, 3}, {1,
            2, 3}}, {{3}, {2, 3}, {1, 2, 3}}}







            share|improve this answer




























              1














              ClearAll[chainedQ]
              chainedQ = And @@ BlockMap[Or @@ Equal @@@ Transpose@#[[All, {1, -1}]] &, #, 2, 1] &;

              Select[list, chainedQ]



              {{{1}, {1, 2}, {1, 2, 3}}, {{2}, {1, 2}, {1, 2, 3}}, {{2}, {2, 3}, {1,
              2, 3}}, {{3}, {2, 3}, {1, 2, 3}}}




              Pick[list, chainedQ /@ list]



              {{{1}, {1, 2}, {1, 2, 3}}, {{2}, {1, 2}, {1, 2, 3}}, {{2}, {2, 3}, {1,
              2, 3}}, {{3}, {2, 3}, {1, 2, 3}}}







              share|improve this answer


























                1












                1








                1







                ClearAll[chainedQ]
                chainedQ = And @@ BlockMap[Or @@ Equal @@@ Transpose@#[[All, {1, -1}]] &, #, 2, 1] &;

                Select[list, chainedQ]



                {{{1}, {1, 2}, {1, 2, 3}}, {{2}, {1, 2}, {1, 2, 3}}, {{2}, {2, 3}, {1,
                2, 3}}, {{3}, {2, 3}, {1, 2, 3}}}




                Pick[list, chainedQ /@ list]



                {{{1}, {1, 2}, {1, 2, 3}}, {{2}, {1, 2}, {1, 2, 3}}, {{2}, {2, 3}, {1,
                2, 3}}, {{3}, {2, 3}, {1, 2, 3}}}







                share|improve this answer













                ClearAll[chainedQ]
                chainedQ = And @@ BlockMap[Or @@ Equal @@@ Transpose@#[[All, {1, -1}]] &, #, 2, 1] &;

                Select[list, chainedQ]



                {{{1}, {1, 2}, {1, 2, 3}}, {{2}, {1, 2}, {1, 2, 3}}, {{2}, {2, 3}, {1,
                2, 3}}, {{3}, {2, 3}, {1, 2, 3}}}




                Pick[list, chainedQ /@ list]



                {{{1}, {1, 2}, {1, 2, 3}}, {{2}, {1, 2}, {1, 2, 3}}, {{2}, {2, 3}, {1,
                2, 3}}, {{3}, {2, 3}, {1, 2, 3}}}








                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 18 hours ago









                kglrkglr

                178k9198409




                178k9198409






























                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f189367%2fselecting-elements-satisfying-a-condition%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

                    Сан-Квентин

                    8-я гвардейская общевойсковая армия

                    Алькесар