Deleting certain integers from string list

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











up vote
8
down vote

favorite












I have a list of strings:



lis = "a","1","b","2","c","3","a","d","4"


and would like to get:



res = "a","b","2","c","3","a","d","4"


where each occurrence of "a" that is immediately followed by (a string representation of) an integer, that integer is deleted from the list. ToExpression followed by IntegerQ seems inefficient, would be grateful for thoughts.










share|improve this question



























    up vote
    8
    down vote

    favorite












    I have a list of strings:



    lis = "a","1","b","2","c","3","a","d","4"


    and would like to get:



    res = "a","b","2","c","3","a","d","4"


    where each occurrence of "a" that is immediately followed by (a string representation of) an integer, that integer is deleted from the list. ToExpression followed by IntegerQ seems inefficient, would be grateful for thoughts.










    share|improve this question

























      up vote
      8
      down vote

      favorite









      up vote
      8
      down vote

      favorite











      I have a list of strings:



      lis = "a","1","b","2","c","3","a","d","4"


      and would like to get:



      res = "a","b","2","c","3","a","d","4"


      where each occurrence of "a" that is immediately followed by (a string representation of) an integer, that integer is deleted from the list. ToExpression followed by IntegerQ seems inefficient, would be grateful for thoughts.










      share|improve this question















      I have a list of strings:



      lis = "a","1","b","2","c","3","a","d","4"


      and would like to get:



      res = "a","b","2","c","3","a","d","4"


      where each occurrence of "a" that is immediately followed by (a string representation of) an integer, that integer is deleted from the list. ToExpression followed by IntegerQ seems inefficient, would be grateful for thoughts.







      list-manipulation string-manipulation






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 days ago









      kglr

      159k8184384




      159k8184384










      asked 2 days ago









      Suite401

      864212




      864212




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          8
          down vote



          accepted










          SequenceReplace:



          SequenceReplace[lis, "a", _?(StringMatchQ[NumberString]) :> "a"]



           "a", "b", "2", "c", "3", "a", "d", "4"




          Also



          SequenceReplace[lis, "a", _?(IntegerQ @* ToExpression) :> "a"]



           "a", "b", "2", "c", "3", "a", "d", "4"




          Split + ReplaceAll



          Flatten[Split[lis, # == "a" &] /. "a", _?(IntegerQ@*ToExpression) :> "a"]



           "a", "b", "2", "c", "3", "a", "d", "4"







          share|improve this answer


















          • 1




            Thank you both!
            – Suite401
            2 days ago

















          up vote
          6
          down vote













          The following works for your example. But I am not sure that it will work for you if your example is not descriptive enough for some more general situation you have in mind.



          lis = "a", "1", "b", "2", "c", "3", "a", "d", "4";
          Flatten[Partition[lis, UpTo[2]] /. "a", "1" -> "a"]



          "a", "b", "2", "c", "3", "a", "d", "4"






          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%2f181661%2fdeleting-certain-integers-from-string-list%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            8
            down vote



            accepted










            SequenceReplace:



            SequenceReplace[lis, "a", _?(StringMatchQ[NumberString]) :> "a"]



             "a", "b", "2", "c", "3", "a", "d", "4"




            Also



            SequenceReplace[lis, "a", _?(IntegerQ @* ToExpression) :> "a"]



             "a", "b", "2", "c", "3", "a", "d", "4"




            Split + ReplaceAll



            Flatten[Split[lis, # == "a" &] /. "a", _?(IntegerQ@*ToExpression) :> "a"]



             "a", "b", "2", "c", "3", "a", "d", "4"







            share|improve this answer


















            • 1




              Thank you both!
              – Suite401
              2 days ago














            up vote
            8
            down vote



            accepted










            SequenceReplace:



            SequenceReplace[lis, "a", _?(StringMatchQ[NumberString]) :> "a"]



             "a", "b", "2", "c", "3", "a", "d", "4"




            Also



            SequenceReplace[lis, "a", _?(IntegerQ @* ToExpression) :> "a"]



             "a", "b", "2", "c", "3", "a", "d", "4"




            Split + ReplaceAll



            Flatten[Split[lis, # == "a" &] /. "a", _?(IntegerQ@*ToExpression) :> "a"]



             "a", "b", "2", "c", "3", "a", "d", "4"







            share|improve this answer


















            • 1




              Thank you both!
              – Suite401
              2 days ago












            up vote
            8
            down vote



            accepted







            up vote
            8
            down vote



            accepted






            SequenceReplace:



            SequenceReplace[lis, "a", _?(StringMatchQ[NumberString]) :> "a"]



             "a", "b", "2", "c", "3", "a", "d", "4"




            Also



            SequenceReplace[lis, "a", _?(IntegerQ @* ToExpression) :> "a"]



             "a", "b", "2", "c", "3", "a", "d", "4"




            Split + ReplaceAll



            Flatten[Split[lis, # == "a" &] /. "a", _?(IntegerQ@*ToExpression) :> "a"]



             "a", "b", "2", "c", "3", "a", "d", "4"







            share|improve this answer














            SequenceReplace:



            SequenceReplace[lis, "a", _?(StringMatchQ[NumberString]) :> "a"]



             "a", "b", "2", "c", "3", "a", "d", "4"




            Also



            SequenceReplace[lis, "a", _?(IntegerQ @* ToExpression) :> "a"]



             "a", "b", "2", "c", "3", "a", "d", "4"




            Split + ReplaceAll



            Flatten[Split[lis, # == "a" &] /. "a", _?(IntegerQ@*ToExpression) :> "a"]



             "a", "b", "2", "c", "3", "a", "d", "4"








            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 2 days ago

























            answered 2 days ago









            kglr

            159k8184384




            159k8184384







            • 1




              Thank you both!
              – Suite401
              2 days ago












            • 1




              Thank you both!
              – Suite401
              2 days ago







            1




            1




            Thank you both!
            – Suite401
            2 days ago




            Thank you both!
            – Suite401
            2 days ago










            up vote
            6
            down vote













            The following works for your example. But I am not sure that it will work for you if your example is not descriptive enough for some more general situation you have in mind.



            lis = "a", "1", "b", "2", "c", "3", "a", "d", "4";
            Flatten[Partition[lis, UpTo[2]] /. "a", "1" -> "a"]



            "a", "b", "2", "c", "3", "a", "d", "4"






            share|improve this answer
























              up vote
              6
              down vote













              The following works for your example. But I am not sure that it will work for you if your example is not descriptive enough for some more general situation you have in mind.



              lis = "a", "1", "b", "2", "c", "3", "a", "d", "4";
              Flatten[Partition[lis, UpTo[2]] /. "a", "1" -> "a"]



              "a", "b", "2", "c", "3", "a", "d", "4"






              share|improve this answer






















                up vote
                6
                down vote










                up vote
                6
                down vote









                The following works for your example. But I am not sure that it will work for you if your example is not descriptive enough for some more general situation you have in mind.



                lis = "a", "1", "b", "2", "c", "3", "a", "d", "4";
                Flatten[Partition[lis, UpTo[2]] /. "a", "1" -> "a"]



                "a", "b", "2", "c", "3", "a", "d", "4"






                share|improve this answer












                The following works for your example. But I am not sure that it will work for you if your example is not descriptive enough for some more general situation you have in mind.



                lis = "a", "1", "b", "2", "c", "3", "a", "d", "4";
                Flatten[Partition[lis, UpTo[2]] /. "a", "1" -> "a"]



                "a", "b", "2", "c", "3", "a", "d", "4"







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 days ago









                m_goldberg

                81.8k869190




                81.8k869190



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181661%2fdeleting-certain-integers-from-string-list%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    Long meetings (6-7 hours a day): Being “babysat” by supervisor

                    Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                    Confectionery