I have a list of replacement rules like
list˘of˘replacement˘rules = {a -> 1, b -> 2, c -> 3}
and I want to filter all the elements where ‘b’ is replaced by using Cases[] and a search pattern.
The following code does not work:
Cases[list˘of˘replacement˘rules, b -> _]
The result is {}.
How should the pattern be defined in order to get the result {b->2}?
Answer
rules = {a -> 1, b -> 2, c -> 3};
FilterRules[rules, b]
(* {b -> 2} )*
or
Cases[{a -> 1, b -> 2, c -> 3}, PatternSequence[b -> _]]
(* {b -> 2} )*
Comments
Post a Comment