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 Sep 11 at 4:09









      kglr

      162k8187386




      162k8187386










      asked Sep 11 at 3:37









      Suite401

      903312




      903312




















          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
            Sep 11 at 4:19

















          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
              Sep 11 at 4:19














            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
              Sep 11 at 4:19












            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 Sep 11 at 4:04

























            answered Sep 11 at 3:51









            kglr

            162k8187386




            162k8187386







            • 1




              Thank you both!
              – Suite401
              Sep 11 at 4:19












            • 1




              Thank you both!
              – Suite401
              Sep 11 at 4:19







            1




            1




            Thank you both!
            – Suite401
            Sep 11 at 4:19




            Thank you both!
            – Suite401
            Sep 11 at 4:19










            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 Sep 11 at 4:02









                m_goldberg

                82.4k869190




                82.4k869190



























                     

                    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













































































                    Popular posts from this blog

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

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay