rule = {beta -> 4, alpha -> 2, x -> 4, z -> 2, w -> 0.8}
Select[rule, beta]
Select[rule, beta &]
Select[rule, beta -> _ &]
How would I select the rule that applies to beta only?
Or more general, how to select the rules apply to alpha and beta (or more variable, but not all of them)?
Thanks.
Answer
You can also use FilterRules:
rule = {beta -> 4, alpha -> 2, x -> 4, z -> 2, w -> 0.8};
FilterRules[rule, beta]
(* {beta -> 4} *)
FilterRules[rule, {beta, alpha}]
(* {beta -> 4, alpha -> 2} *)
Update: additional alternatives if you have V10:
KeyTake[rule,{alpha, x}]
(* or *) KeyTake[{alpha,x}][rule]
(* <|alpha->2,x->4|> *)
Normal@KeyTake[rule,{alpha, x}]
(* {alpha->2,x->4} *)
KeySelect[rule, MatchQ[#,alpha|x]&]
(* or *) KeySelect[MatchQ[#,alpha|x]&][rule]
(* <|alpha->2,x->4|> *)
Normal@KeySelect[rule, MatchQ[#,alpha|x]&]
(* {alpha->2,x->4} *)
Comments
Post a Comment