Selecting elements satisfying a condition












4












$begingroup$


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











$endgroup$

















    4












    $begingroup$


    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











    $endgroup$















      4












      4








      4


      2



      $begingroup$


      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











      $endgroup$




      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 Jan 13 at 8:21









      Mr.Wizard

      232k294791066




      232k294791066










      asked Jan 12 at 20:42









      amator2357amator2357

      1167




      1167






















          2 Answers
          2






          active

          oldest

          votes


















          6












          $begingroup$

          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









          $endgroup$













          • $begingroup$
            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!
            $endgroup$
            – amator2357
            Jan 12 at 21:28



















          1












          $begingroup$

          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









          $endgroup$














            Your Answer








            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












            $begingroup$

            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









            $endgroup$













            • $begingroup$
              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!
              $endgroup$
              – amator2357
              Jan 12 at 21:28
















            6












            $begingroup$

            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









            $endgroup$













            • $begingroup$
              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!
              $endgroup$
              – amator2357
              Jan 12 at 21:28














            6












            6








            6





            $begingroup$

            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









            $endgroup$



            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 Jan 12 at 21:00









            Niki EstnerNiki Estner

            31.1k376133




            31.1k376133












            • $begingroup$
              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!
              $endgroup$
              – amator2357
              Jan 12 at 21:28


















            • $begingroup$
              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!
              $endgroup$
              – amator2357
              Jan 12 at 21:28
















            $begingroup$
            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!
            $endgroup$
            – amator2357
            Jan 12 at 21:28




            $begingroup$
            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!
            $endgroup$
            – amator2357
            Jan 12 at 21:28











            1












            $begingroup$

            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









            $endgroup$


















              1












              $begingroup$

              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









              $endgroup$
















                1












                1








                1





                $begingroup$

                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









                $endgroup$



                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 Jan 13 at 11:32









                kglrkglr

                190k10207425




                190k10207425






























                    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

                    Bressuire

                    Cabo Verde

                    Gyllenstierna