Apply function over a list only if list members satisfy multiple conditions

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











up vote
4
down vote

favorite












I want to apply a function to members of a list only if all the members of the list satisfy a set of conditions otherwise return a result of zero. For example for



list1 = 0, "", " ", 1, 5, 6;
list2 = 1, 4, 3, 6, 7, 9, 9, 11;


the desired output would be



f[list1]
f[list2] (* e.g. Plus@@list *)



0



50




What I've tried so far:



f[list_] := 
If[(# == 0 || N[#] == "" || N[#] == " "), 0,
Plus @@ list] & /@ list
f[list1]
f[list2]



0, 0, 0, 12 + "" + " ", 12 + "" + " ", 12 + "" + " "
50, 50, 50, 50, 50, 50, 50, 50




It's clear to me why this didn't work but I can't quite see the appropriate chicanery. Any suggestions?







share|improve this question
























    up vote
    4
    down vote

    favorite












    I want to apply a function to members of a list only if all the members of the list satisfy a set of conditions otherwise return a result of zero. For example for



    list1 = 0, "", " ", 1, 5, 6;
    list2 = 1, 4, 3, 6, 7, 9, 9, 11;


    the desired output would be



    f[list1]
    f[list2] (* e.g. Plus@@list *)



    0



    50




    What I've tried so far:



    f[list_] := 
    If[(# == 0 || N[#] == "" || N[#] == " "), 0,
    Plus @@ list] & /@ list
    f[list1]
    f[list2]



    0, 0, 0, 12 + "" + " ", 12 + "" + " ", 12 + "" + " "
    50, 50, 50, 50, 50, 50, 50, 50




    It's clear to me why this didn't work but I can't quite see the appropriate chicanery. Any suggestions?







    share|improve this question






















      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      I want to apply a function to members of a list only if all the members of the list satisfy a set of conditions otherwise return a result of zero. For example for



      list1 = 0, "", " ", 1, 5, 6;
      list2 = 1, 4, 3, 6, 7, 9, 9, 11;


      the desired output would be



      f[list1]
      f[list2] (* e.g. Plus@@list *)



      0



      50




      What I've tried so far:



      f[list_] := 
      If[(# == 0 || N[#] == "" || N[#] == " "), 0,
      Plus @@ list] & /@ list
      f[list1]
      f[list2]



      0, 0, 0, 12 + "" + " ", 12 + "" + " ", 12 + "" + " "
      50, 50, 50, 50, 50, 50, 50, 50




      It's clear to me why this didn't work but I can't quite see the appropriate chicanery. Any suggestions?







      share|improve this question












      I want to apply a function to members of a list only if all the members of the list satisfy a set of conditions otherwise return a result of zero. For example for



      list1 = 0, "", " ", 1, 5, 6;
      list2 = 1, 4, 3, 6, 7, 9, 9, 11;


      the desired output would be



      f[list1]
      f[list2] (* e.g. Plus@@list *)



      0



      50




      What I've tried so far:



      f[list_] := 
      If[(# == 0 || N[#] == "" || N[#] == " "), 0,
      Plus @@ list] & /@ list
      f[list1]
      f[list2]



      0, 0, 0, 12 + "" + " ", 12 + "" + " ", 12 + "" + " "
      50, 50, 50, 50, 50, 50, 50, 50




      It's clear to me why this didn't work but I can't quite see the appropriate chicanery. Any suggestions?









      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 7 at 0:34









      geordie

      1,9141430




      1,9141430




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          ClearAll[f]
          f[list_] := If[FreeQ[list, 0 | "" | " "], Plus @@ list, 0]

          f[list1]



          0




          f[list2]



          50




          Alternatively,



          ClearAll[f2]
          f2[list_] := Boole[FreeQ[list, 0 | "" | " "]] Plus @@ list;

          f2[list1]



          0




          f2[list2]



          50




          Or



          ClearAll[f3]
          f3[lst : Except[Alternatives[0, "", " "]] ..] := Plus @@ lst
          f3[__] := 0

          f3[list1]



          0




          f3[list2]



          50







          share|improve this answer





























            up vote
            2
            down vote













            Another way to build the function is:



            g[x_] := If[Length@Select[x, NumberQ] == Length[x], Total[x], 0];
            g[list1], g[list2]
            0, 50





            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%2f181382%2fapply-function-over-a-list-only-if-list-members-satisfy-multiple-conditions%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
              3
              down vote



              accepted










              ClearAll[f]
              f[list_] := If[FreeQ[list, 0 | "" | " "], Plus @@ list, 0]

              f[list1]



              0




              f[list2]



              50




              Alternatively,



              ClearAll[f2]
              f2[list_] := Boole[FreeQ[list, 0 | "" | " "]] Plus @@ list;

              f2[list1]



              0




              f2[list2]



              50




              Or



              ClearAll[f3]
              f3[lst : Except[Alternatives[0, "", " "]] ..] := Plus @@ lst
              f3[__] := 0

              f3[list1]



              0




              f3[list2]



              50







              share|improve this answer


























                up vote
                3
                down vote



                accepted










                ClearAll[f]
                f[list_] := If[FreeQ[list, 0 | "" | " "], Plus @@ list, 0]

                f[list1]



                0




                f[list2]



                50




                Alternatively,



                ClearAll[f2]
                f2[list_] := Boole[FreeQ[list, 0 | "" | " "]] Plus @@ list;

                f2[list1]



                0




                f2[list2]



                50




                Or



                ClearAll[f3]
                f3[lst : Except[Alternatives[0, "", " "]] ..] := Plus @@ lst
                f3[__] := 0

                f3[list1]



                0




                f3[list2]



                50







                share|improve this answer
























                  up vote
                  3
                  down vote



                  accepted







                  up vote
                  3
                  down vote



                  accepted






                  ClearAll[f]
                  f[list_] := If[FreeQ[list, 0 | "" | " "], Plus @@ list, 0]

                  f[list1]



                  0




                  f[list2]



                  50




                  Alternatively,



                  ClearAll[f2]
                  f2[list_] := Boole[FreeQ[list, 0 | "" | " "]] Plus @@ list;

                  f2[list1]



                  0




                  f2[list2]



                  50




                  Or



                  ClearAll[f3]
                  f3[lst : Except[Alternatives[0, "", " "]] ..] := Plus @@ lst
                  f3[__] := 0

                  f3[list1]



                  0




                  f3[list2]



                  50







                  share|improve this answer














                  ClearAll[f]
                  f[list_] := If[FreeQ[list, 0 | "" | " "], Plus @@ list, 0]

                  f[list1]



                  0




                  f[list2]



                  50




                  Alternatively,



                  ClearAll[f2]
                  f2[list_] := Boole[FreeQ[list, 0 | "" | " "]] Plus @@ list;

                  f2[list1]



                  0




                  f2[list2]



                  50




                  Or



                  ClearAll[f3]
                  f3[lst : Except[Alternatives[0, "", " "]] ..] := Plus @@ lst
                  f3[__] := 0

                  f3[list1]



                  0




                  f3[list2]



                  50








                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Sep 7 at 1:09

























                  answered Sep 7 at 0:46









                  kglr

                  159k8183383




                  159k8183383




















                      up vote
                      2
                      down vote













                      Another way to build the function is:



                      g[x_] := If[Length@Select[x, NumberQ] == Length[x], Total[x], 0];
                      g[list1], g[list2]
                      0, 50





                      share|improve this answer
























                        up vote
                        2
                        down vote













                        Another way to build the function is:



                        g[x_] := If[Length@Select[x, NumberQ] == Length[x], Total[x], 0];
                        g[list1], g[list2]
                        0, 50





                        share|improve this answer






















                          up vote
                          2
                          down vote










                          up vote
                          2
                          down vote









                          Another way to build the function is:



                          g[x_] := If[Length@Select[x, NumberQ] == Length[x], Total[x], 0];
                          g[list1], g[list2]
                          0, 50





                          share|improve this answer












                          Another way to build the function is:



                          g[x_] := If[Length@Select[x, NumberQ] == Length[x], Total[x], 0];
                          g[list1], g[list2]
                          0, 50






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Sep 7 at 3:02









                          bill s

                          50.7k373143




                          50.7k373143



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181382%2fapply-function-over-a-list-only-if-list-members-satisfy-multiple-conditions%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Comments

                              Popular posts from this blog

                              What does second last employer means? [closed]

                              Installing NextGIS Connect into QGIS 3?

                              One-line joke