9
$\begingroup$
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.

$\endgroup$
17
$\begingroup$

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} *)
| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Yeah, I certainly need more sleep... $\endgroup$ – Kuba Dec 18 '14 at 16:23
  • $\begingroup$ @Kuba Why? Your method is perfectly adequate. $\endgroup$ – Mr.Wizard Dec 18 '14 at 17:05
  • $\begingroup$ Nice use of KeyTake. That one isn't in my repertoire yet. $\endgroup$ – Mr.Wizard Dec 18 '14 at 17:43
  • $\begingroup$ @Mr.Wizard, thank you. Added to my repertoire a minute ago:) $\endgroup$ – kglr Dec 18 '14 at 17:44
  • 1
    $\begingroup$ @Mr.Wizard, do we need a similar warning for KeySelect? (I am getting the message "You have exceeded the session memory limit for your plan" when i try to run KeySelect[MatchQ[#,b]&][{a -> b, b -> c, b -> 2}] on the free wolfram programming cloud). $\endgroup$ – kglr Dec 18 '14 at 17:56
6
$\begingroup$
Select[rule, MemberQ[{beta, alpha}, #[[1]]] &]
| improve this answer | |
$\endgroup$
5
$\begingroup$

V9 style

If rule is a simple list of rules:

Cases[rule, _[beta | alpha, _]]
{beta -> 4, alpha -> 2}

other ways to go:

{beta, alpha} /. (# -> Rule[##] & @@@ rule)
| improve this answer | |
$\endgroup$
  • $\begingroup$ First approach is simple to understand. second approach looks more complicated though. $\endgroup$ – Chen Stats Yu Dec 18 '14 at 15:52
  • 1
    $\begingroup$ @ChenStatsYu Take a look at (# -> Rule[##] & @@@ rule) $\endgroup$ – Kuba Dec 18 '14 at 15:54
  • $\begingroup$ @kuba I am trying to replicate your first solution as follows: Cases[rule, Rule[beta | alpha, _]] but it did not work. do you know why? this works fine Cases[{{a, 3}, {b, 4}, {c, 5}}, List[a | b, _]] $\endgroup$ – Algohi Dec 18 '14 at 16:36
  • $\begingroup$ @Algohi Rule makes Cases think that you want to replace beta|alpha with _. Since by default Cases works on level {1} it can't find anything anyway. To make use of explicitly stated Rule you need HoldPattern[...] $\endgroup$ – Kuba Dec 18 '14 at 16:42
4
$\begingroup$

One more method using Pick and Keys:

Pick[rule, Keys@rule, alpha | beta]
{beta -> 4, alpha -> 2}
| improve this answer | |
$\endgroup$
4
$\begingroup$
Rule[#, (Association@rule)[#]] & /@ {beta, alpha}

{beta -> 4, alpha -> 2}

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.