Deleting items from a list when they meet certain criteria

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
5
down vote

favorite












I have a list:



lis = "a", "xx", "b", "c", "18", "d", "a", "23", "b", "w", "xx", "z"


I would like to obtain:



res = "c", 18, "d", "a", "23", "b", "w", "xx", "z"


where items are dropped from lis if their middle sub-element is "xx" and another element in lis contains the same first and last sub-elements and has digit characters in its middle sub-element. Sorting by lis[[1]] puts the two almost identical items next to each other, but I'm not sure how to use DeleteCases here.










share|improve this question



























    up vote
    5
    down vote

    favorite












    I have a list:



    lis = "a", "xx", "b", "c", "18", "d", "a", "23", "b", "w", "xx", "z"


    I would like to obtain:



    res = "c", 18, "d", "a", "23", "b", "w", "xx", "z"


    where items are dropped from lis if their middle sub-element is "xx" and another element in lis contains the same first and last sub-elements and has digit characters in its middle sub-element. Sorting by lis[[1]] puts the two almost identical items next to each other, but I'm not sure how to use DeleteCases here.










    share|improve this question

























      up vote
      5
      down vote

      favorite









      up vote
      5
      down vote

      favorite











      I have a list:



      lis = "a", "xx", "b", "c", "18", "d", "a", "23", "b", "w", "xx", "z"


      I would like to obtain:



      res = "c", 18, "d", "a", "23", "b", "w", "xx", "z"


      where items are dropped from lis if their middle sub-element is "xx" and another element in lis contains the same first and last sub-elements and has digit characters in its middle sub-element. Sorting by lis[[1]] puts the two almost identical items next to each other, but I'm not sure how to use DeleteCases here.










      share|improve this question















      I have a list:



      lis = "a", "xx", "b", "c", "18", "d", "a", "23", "b", "w", "xx", "z"


      I would like to obtain:



      res = "c", 18, "d", "a", "23", "b", "w", "xx", "z"


      where items are dropped from lis if their middle sub-element is "xx" and another element in lis contains the same first and last sub-elements and has digit characters in its middle sub-element. Sorting by lis[[1]] puts the two almost identical items next to each other, but I'm not sure how to use DeleteCases here.







      list-manipulation






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 8 at 4:02









      m_goldberg

      82.3k869190




      82.3k869190










      asked Sep 8 at 2:55









      Suite401

      903312




      903312




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          7
          down vote



          accepted










          Union:



          Union[lis, SameTest -> (And[#[[2]] == "xx", #[[1, 3]] == #2[[1, 3]]] &)]



          "a", "23", "b", "c", "18", "d", "w", "xx", "z"




          DeleteCases:



          cond = And[#[[2]] == "xx" , 
          MatchQ[#[[1, 3]], Alternatives @@ Complement[lis, #][[All, 1, 3]]]] &;
          DeleteCases[lis, _?cond]



          "c", "18", "d", "a", "23", "b", "w", "xx", "z"




          A combination of GatherBy and DeleteCases:



          Join @@ If[Length @ # > 1, DeleteCases[#, _, "xx", _], #] & /@ 
          GatherBy[lis, #[[1, 3]] &]



          "a", "23", "b", "c", "18", "d", "w", "xx", "z"







          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: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            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%2f181468%2fdeleting-items-from-a-list-when-they-meet-certain-criteria%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            7
            down vote



            accepted










            Union:



            Union[lis, SameTest -> (And[#[[2]] == "xx", #[[1, 3]] == #2[[1, 3]]] &)]



            "a", "23", "b", "c", "18", "d", "w", "xx", "z"




            DeleteCases:



            cond = And[#[[2]] == "xx" , 
            MatchQ[#[[1, 3]], Alternatives @@ Complement[lis, #][[All, 1, 3]]]] &;
            DeleteCases[lis, _?cond]



            "c", "18", "d", "a", "23", "b", "w", "xx", "z"




            A combination of GatherBy and DeleteCases:



            Join @@ If[Length @ # > 1, DeleteCases[#, _, "xx", _], #] & /@ 
            GatherBy[lis, #[[1, 3]] &]



            "a", "23", "b", "c", "18", "d", "w", "xx", "z"







            share|improve this answer


























              up vote
              7
              down vote



              accepted










              Union:



              Union[lis, SameTest -> (And[#[[2]] == "xx", #[[1, 3]] == #2[[1, 3]]] &)]



              "a", "23", "b", "c", "18", "d", "w", "xx", "z"




              DeleteCases:



              cond = And[#[[2]] == "xx" , 
              MatchQ[#[[1, 3]], Alternatives @@ Complement[lis, #][[All, 1, 3]]]] &;
              DeleteCases[lis, _?cond]



              "c", "18", "d", "a", "23", "b", "w", "xx", "z"




              A combination of GatherBy and DeleteCases:



              Join @@ If[Length @ # > 1, DeleteCases[#, _, "xx", _], #] & /@ 
              GatherBy[lis, #[[1, 3]] &]



              "a", "23", "b", "c", "18", "d", "w", "xx", "z"







              share|improve this answer
























                up vote
                7
                down vote



                accepted







                up vote
                7
                down vote



                accepted






                Union:



                Union[lis, SameTest -> (And[#[[2]] == "xx", #[[1, 3]] == #2[[1, 3]]] &)]



                "a", "23", "b", "c", "18", "d", "w", "xx", "z"




                DeleteCases:



                cond = And[#[[2]] == "xx" , 
                MatchQ[#[[1, 3]], Alternatives @@ Complement[lis, #][[All, 1, 3]]]] &;
                DeleteCases[lis, _?cond]



                "c", "18", "d", "a", "23", "b", "w", "xx", "z"




                A combination of GatherBy and DeleteCases:



                Join @@ If[Length @ # > 1, DeleteCases[#, _, "xx", _], #] & /@ 
                GatherBy[lis, #[[1, 3]] &]



                "a", "23", "b", "c", "18", "d", "w", "xx", "z"







                share|improve this answer














                Union:



                Union[lis, SameTest -> (And[#[[2]] == "xx", #[[1, 3]] == #2[[1, 3]]] &)]



                "a", "23", "b", "c", "18", "d", "w", "xx", "z"




                DeleteCases:



                cond = And[#[[2]] == "xx" , 
                MatchQ[#[[1, 3]], Alternatives @@ Complement[lis, #][[All, 1, 3]]]] &;
                DeleteCases[lis, _?cond]



                "c", "18", "d", "a", "23", "b", "w", "xx", "z"




                A combination of GatherBy and DeleteCases:



                Join @@ If[Length @ # > 1, DeleteCases[#, _, "xx", _], #] & /@ 
                GatherBy[lis, #[[1, 3]] &]



                "a", "23", "b", "c", "18", "d", "w", "xx", "z"








                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Sep 8 at 6:07

























                answered Sep 8 at 3:31









                kglr

                162k8187386




                162k8187386



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181468%2fdeleting-items-from-a-list-when-they-meet-certain-criteria%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?