Apply function over a list only if list members satisfy multiple conditions
Clash 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?
list-manipulation conditional
add a comment |Â
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?
list-manipulation conditional
add a comment |Â
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?
list-manipulation conditional
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?
list-manipulation conditional
asked Sep 7 at 0:34
geordie
1,9141430
1,9141430
add a comment |Â
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
edited Sep 7 at 1:09
answered Sep 7 at 0:46
kglr
159k8183383
159k8183383
add a comment |Â
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered Sep 7 at 3:02
bill s
50.7k373143
50.7k373143
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password