Replace targeted elements in first level of list only

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











up vote
2
down vote

favorite












I have a list where some elements are strings or sublists:



list = "DTLCIGYHANNSTDT", "LCLGHHAVPNGTLVKTITNDQIEVTNATELVQSSSTGKI", 
"CLGHHAVPNGTLVKTITNDQIEVTNATELVQSSSTGKIC", "H25", " H45", " I361",
" D362", "N197", "H25", " H45", " S46", " V47", " T332", " V361", " D362",
" G363", " W364", " Q381", " K382", " T384", " Q385", " I388",
" N389", " V395", " N396", " I399", "H25", " H45", " V47", " N48",
" L49", " T332", " D362", " G363", " W364", " Q381", " T384",
" Q385", " I388", " N389", " T392", " V395", " N396", " I399",
" E400", "NSTDTVDTVLEKNVT", "D31", " S46", "S145", ...


I want to target the 1st level strings that look like "N197" or "S145" and replace them with N197 and S145. Transform from String to List, essentially.



I've tried with



list /. 
x_String /;
StringMatchQ[x, RegularExpression["^[A-Z]\d1,3$"]] :> List[x]


This command affects the desired elements, PLUS the first string element of every sublist. I know I'm missing something obvious, but how can I keep the changes to the 1st-level of the list?










share|improve this question



























    up vote
    2
    down vote

    favorite












    I have a list where some elements are strings or sublists:



    list = "DTLCIGYHANNSTDT", "LCLGHHAVPNGTLVKTITNDQIEVTNATELVQSSSTGKI", 
    "CLGHHAVPNGTLVKTITNDQIEVTNATELVQSSSTGKIC", "H25", " H45", " I361",
    " D362", "N197", "H25", " H45", " S46", " V47", " T332", " V361", " D362",
    " G363", " W364", " Q381", " K382", " T384", " Q385", " I388",
    " N389", " V395", " N396", " I399", "H25", " H45", " V47", " N48",
    " L49", " T332", " D362", " G363", " W364", " Q381", " T384",
    " Q385", " I388", " N389", " T392", " V395", " N396", " I399",
    " E400", "NSTDTVDTVLEKNVT", "D31", " S46", "S145", ...


    I want to target the 1st level strings that look like "N197" or "S145" and replace them with N197 and S145. Transform from String to List, essentially.



    I've tried with



    list /. 
    x_String /;
    StringMatchQ[x, RegularExpression["^[A-Z]\d1,3$"]] :> List[x]


    This command affects the desired elements, PLUS the first string element of every sublist. I know I'm missing something obvious, but how can I keep the changes to the 1st-level of the list?










    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have a list where some elements are strings or sublists:



      list = "DTLCIGYHANNSTDT", "LCLGHHAVPNGTLVKTITNDQIEVTNATELVQSSSTGKI", 
      "CLGHHAVPNGTLVKTITNDQIEVTNATELVQSSSTGKIC", "H25", " H45", " I361",
      " D362", "N197", "H25", " H45", " S46", " V47", " T332", " V361", " D362",
      " G363", " W364", " Q381", " K382", " T384", " Q385", " I388",
      " N389", " V395", " N396", " I399", "H25", " H45", " V47", " N48",
      " L49", " T332", " D362", " G363", " W364", " Q381", " T384",
      " Q385", " I388", " N389", " T392", " V395", " N396", " I399",
      " E400", "NSTDTVDTVLEKNVT", "D31", " S46", "S145", ...


      I want to target the 1st level strings that look like "N197" or "S145" and replace them with N197 and S145. Transform from String to List, essentially.



      I've tried with



      list /. 
      x_String /;
      StringMatchQ[x, RegularExpression["^[A-Z]\d1,3$"]] :> List[x]


      This command affects the desired elements, PLUS the first string element of every sublist. I know I'm missing something obvious, but how can I keep the changes to the 1st-level of the list?










      share|improve this question















      I have a list where some elements are strings or sublists:



      list = "DTLCIGYHANNSTDT", "LCLGHHAVPNGTLVKTITNDQIEVTNATELVQSSSTGKI", 
      "CLGHHAVPNGTLVKTITNDQIEVTNATELVQSSSTGKIC", "H25", " H45", " I361",
      " D362", "N197", "H25", " H45", " S46", " V47", " T332", " V361", " D362",
      " G363", " W364", " Q381", " K382", " T384", " Q385", " I388",
      " N389", " V395", " N396", " I399", "H25", " H45", " V47", " N48",
      " L49", " T332", " D362", " G363", " W364", " Q381", " T384",
      " Q385", " I388", " N389", " T392", " V395", " N396", " I399",
      " E400", "NSTDTVDTVLEKNVT", "D31", " S46", "S145", ...


      I want to target the 1st level strings that look like "N197" or "S145" and replace them with N197 and S145. Transform from String to List, essentially.



      I've tried with



      list /. 
      x_String /;
      StringMatchQ[x, RegularExpression["^[A-Z]\d1,3$"]] :> List[x]


      This command affects the desired elements, PLUS the first string element of every sublist. I know I'm missing something obvious, but how can I keep the changes to the 1st-level of the list?







      list-manipulation replacement






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 12 mins ago

























      asked 23 mins ago









      briennakh

      2647




      2647




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          Do not use ReplaceAll when you need a level spec. Use Replace. Avoid in general using ReplaceAll in favor of Replace in complex data structures unless you know for sure what is happening and what side-effects ReplaceAll can bring as you keep building your application and growing your code.



          pattern = x_String /; StringMatchQ[x,RegularExpression["^[A-Z]\d1,3$"]];

          Replace[list, pattern :> List[x], 1]





          share|improve this answer




















          • Thanks for the tip about ReplaceAll!
            – briennakh
            13 mins ago










          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%2f183330%2freplace-targeted-elements-in-first-level-of-list-only%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
          3
          down vote



          accepted










          Do not use ReplaceAll when you need a level spec. Use Replace. Avoid in general using ReplaceAll in favor of Replace in complex data structures unless you know for sure what is happening and what side-effects ReplaceAll can bring as you keep building your application and growing your code.



          pattern = x_String /; StringMatchQ[x,RegularExpression["^[A-Z]\d1,3$"]];

          Replace[list, pattern :> List[x], 1]





          share|improve this answer




















          • Thanks for the tip about ReplaceAll!
            – briennakh
            13 mins ago














          up vote
          3
          down vote



          accepted










          Do not use ReplaceAll when you need a level spec. Use Replace. Avoid in general using ReplaceAll in favor of Replace in complex data structures unless you know for sure what is happening and what side-effects ReplaceAll can bring as you keep building your application and growing your code.



          pattern = x_String /; StringMatchQ[x,RegularExpression["^[A-Z]\d1,3$"]];

          Replace[list, pattern :> List[x], 1]





          share|improve this answer




















          • Thanks for the tip about ReplaceAll!
            – briennakh
            13 mins ago












          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          Do not use ReplaceAll when you need a level spec. Use Replace. Avoid in general using ReplaceAll in favor of Replace in complex data structures unless you know for sure what is happening and what side-effects ReplaceAll can bring as you keep building your application and growing your code.



          pattern = x_String /; StringMatchQ[x,RegularExpression["^[A-Z]\d1,3$"]];

          Replace[list, pattern :> List[x], 1]





          share|improve this answer












          Do not use ReplaceAll when you need a level spec. Use Replace. Avoid in general using ReplaceAll in favor of Replace in complex data structures unless you know for sure what is happening and what side-effects ReplaceAll can bring as you keep building your application and growing your code.



          pattern = x_String /; StringMatchQ[x,RegularExpression["^[A-Z]\d1,3$"]];

          Replace[list, pattern :> List[x], 1]






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 16 mins ago









          Vitaliy Kaurov

          56.2k6157275




          56.2k6157275











          • Thanks for the tip about ReplaceAll!
            – briennakh
            13 mins ago
















          • Thanks for the tip about ReplaceAll!
            – briennakh
            13 mins ago















          Thanks for the tip about ReplaceAll!
          – briennakh
          13 mins ago




          Thanks for the tip about ReplaceAll!
          – briennakh
          13 mins ago

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f183330%2freplace-targeted-elements-in-first-level-of-list-only%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