How can I replace non-reals in a multidimensional list?

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











up vote
2
down vote

favorite












Ok. I'm obviously not understanding something about pattern matching.



I have a list called t like the following:



t = 3.2,5.9,Indeterminate,4.5,-3 - 100 (1 - 2/(Underflow + 1)) + 200 (1 - Underflow),7.9,1.2,9.1,2.3


Where I would like to use ReplaceAll to convert all non-real values to 0.0, like so:



3.2,5.9,0.0,4.5,0.0,7.9,1.2,9.1,2.3


How could I do this with ReplaceAll and pattern matching?



I have tried:



t /. r_ /; Head[r] != List | Real -> 0.0


But that does nothing to the list. I am trying to wrap my head around pattern matching, but I'm not quite there yet, and any help would be greatly appreciated.



Thanks!



P.S.- I would really like to understand this in context of pattern matching, but other more efficient ways of doing this are also appreciated!










share|improve this question



























    up vote
    2
    down vote

    favorite












    Ok. I'm obviously not understanding something about pattern matching.



    I have a list called t like the following:



    t = 3.2,5.9,Indeterminate,4.5,-3 - 100 (1 - 2/(Underflow + 1)) + 200 (1 - Underflow),7.9,1.2,9.1,2.3


    Where I would like to use ReplaceAll to convert all non-real values to 0.0, like so:



    3.2,5.9,0.0,4.5,0.0,7.9,1.2,9.1,2.3


    How could I do this with ReplaceAll and pattern matching?



    I have tried:



    t /. r_ /; Head[r] != List | Real -> 0.0


    But that does nothing to the list. I am trying to wrap my head around pattern matching, but I'm not quite there yet, and any help would be greatly appreciated.



    Thanks!



    P.S.- I would really like to understand this in context of pattern matching, but other more efficient ways of doing this are also appreciated!










    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Ok. I'm obviously not understanding something about pattern matching.



      I have a list called t like the following:



      t = 3.2,5.9,Indeterminate,4.5,-3 - 100 (1 - 2/(Underflow + 1)) + 200 (1 - Underflow),7.9,1.2,9.1,2.3


      Where I would like to use ReplaceAll to convert all non-real values to 0.0, like so:



      3.2,5.9,0.0,4.5,0.0,7.9,1.2,9.1,2.3


      How could I do this with ReplaceAll and pattern matching?



      I have tried:



      t /. r_ /; Head[r] != List | Real -> 0.0


      But that does nothing to the list. I am trying to wrap my head around pattern matching, but I'm not quite there yet, and any help would be greatly appreciated.



      Thanks!



      P.S.- I would really like to understand this in context of pattern matching, but other more efficient ways of doing this are also appreciated!










      share|improve this question















      Ok. I'm obviously not understanding something about pattern matching.



      I have a list called t like the following:



      t = 3.2,5.9,Indeterminate,4.5,-3 - 100 (1 - 2/(Underflow + 1)) + 200 (1 - Underflow),7.9,1.2,9.1,2.3


      Where I would like to use ReplaceAll to convert all non-real values to 0.0, like so:



      3.2,5.9,0.0,4.5,0.0,7.9,1.2,9.1,2.3


      How could I do this with ReplaceAll and pattern matching?



      I have tried:



      t /. r_ /; Head[r] != List | Real -> 0.0


      But that does nothing to the list. I am trying to wrap my head around pattern matching, but I'm not quite there yet, and any help would be greatly appreciated.



      Thanks!



      P.S.- I would really like to understand this in context of pattern matching, but other more efficient ways of doing this are also appreciated!







      list-manipulation pattern-matching replacement






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 3 hours ago









      Carl Woll

      57.5k273149




      57.5k273149










      asked 3 hours ago









      Jmeeks29ig

      46228




      46228




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          You had two issues.



          1. You can't use Unequal ("!=") with Alternatives ("|").

          2. Even if you correct your pattern, the pattern would match the whole expression.

          Using Replace with a level spec and a corrected pattern:



          t = 3.2,5.9,Indeterminate,4.5,-3-100 (1-2/(Underflow+1))+200 (1-Underflow),7.9,1.2,9.1,2.3;

          Replace[
          t,
          r_ /; !MatchQ[Head[r], List | Real] -> 0.0,
          2
          ]



          3.2, 5.9, 0., 4.5, 0., 7.9, 1.2, 9.1, 2.3




          Another possibility:



          Replace[
          t,
          Except[_?NumberQ] -> 0.,
          2
          ]



          3.2, 5.9, 0., 4.5, 0., 7.9, 1.2, 9.1, 2.3







          share|improve this answer




















          • Great, thanks! That helps me to understand it a lot better
            – Jmeeks29ig
            1 hour 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%2f182213%2fhow-can-i-replace-non-reals-in-a-multidimensional-list%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
          4
          down vote



          accepted










          You had two issues.



          1. You can't use Unequal ("!=") with Alternatives ("|").

          2. Even if you correct your pattern, the pattern would match the whole expression.

          Using Replace with a level spec and a corrected pattern:



          t = 3.2,5.9,Indeterminate,4.5,-3-100 (1-2/(Underflow+1))+200 (1-Underflow),7.9,1.2,9.1,2.3;

          Replace[
          t,
          r_ /; !MatchQ[Head[r], List | Real] -> 0.0,
          2
          ]



          3.2, 5.9, 0., 4.5, 0., 7.9, 1.2, 9.1, 2.3




          Another possibility:



          Replace[
          t,
          Except[_?NumberQ] -> 0.,
          2
          ]



          3.2, 5.9, 0., 4.5, 0., 7.9, 1.2, 9.1, 2.3







          share|improve this answer




















          • Great, thanks! That helps me to understand it a lot better
            – Jmeeks29ig
            1 hour ago














          up vote
          4
          down vote



          accepted










          You had two issues.



          1. You can't use Unequal ("!=") with Alternatives ("|").

          2. Even if you correct your pattern, the pattern would match the whole expression.

          Using Replace with a level spec and a corrected pattern:



          t = 3.2,5.9,Indeterminate,4.5,-3-100 (1-2/(Underflow+1))+200 (1-Underflow),7.9,1.2,9.1,2.3;

          Replace[
          t,
          r_ /; !MatchQ[Head[r], List | Real] -> 0.0,
          2
          ]



          3.2, 5.9, 0., 4.5, 0., 7.9, 1.2, 9.1, 2.3




          Another possibility:



          Replace[
          t,
          Except[_?NumberQ] -> 0.,
          2
          ]



          3.2, 5.9, 0., 4.5, 0., 7.9, 1.2, 9.1, 2.3







          share|improve this answer




















          • Great, thanks! That helps me to understand it a lot better
            – Jmeeks29ig
            1 hour ago












          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          You had two issues.



          1. You can't use Unequal ("!=") with Alternatives ("|").

          2. Even if you correct your pattern, the pattern would match the whole expression.

          Using Replace with a level spec and a corrected pattern:



          t = 3.2,5.9,Indeterminate,4.5,-3-100 (1-2/(Underflow+1))+200 (1-Underflow),7.9,1.2,9.1,2.3;

          Replace[
          t,
          r_ /; !MatchQ[Head[r], List | Real] -> 0.0,
          2
          ]



          3.2, 5.9, 0., 4.5, 0., 7.9, 1.2, 9.1, 2.3




          Another possibility:



          Replace[
          t,
          Except[_?NumberQ] -> 0.,
          2
          ]



          3.2, 5.9, 0., 4.5, 0., 7.9, 1.2, 9.1, 2.3







          share|improve this answer












          You had two issues.



          1. You can't use Unequal ("!=") with Alternatives ("|").

          2. Even if you correct your pattern, the pattern would match the whole expression.

          Using Replace with a level spec and a corrected pattern:



          t = 3.2,5.9,Indeterminate,4.5,-3-100 (1-2/(Underflow+1))+200 (1-Underflow),7.9,1.2,9.1,2.3;

          Replace[
          t,
          r_ /; !MatchQ[Head[r], List | Real] -> 0.0,
          2
          ]



          3.2, 5.9, 0., 4.5, 0., 7.9, 1.2, 9.1, 2.3




          Another possibility:



          Replace[
          t,
          Except[_?NumberQ] -> 0.,
          2
          ]



          3.2, 5.9, 0., 4.5, 0., 7.9, 1.2, 9.1, 2.3








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 3 hours ago









          Carl Woll

          57.5k273149




          57.5k273149











          • Great, thanks! That helps me to understand it a lot better
            – Jmeeks29ig
            1 hour ago
















          • Great, thanks! That helps me to understand it a lot better
            – Jmeeks29ig
            1 hour ago















          Great, thanks! That helps me to understand it a lot better
          – Jmeeks29ig
          1 hour ago




          Great, thanks! That helps me to understand it a lot better
          – Jmeeks29ig
          1 hour 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%2f182213%2fhow-can-i-replace-non-reals-in-a-multidimensional-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