4
$\begingroup$

Let's say I have a list of replacement rules:

List1 = {2 -> 3, b -> 5.5, 6.57 -> 2, 3 -> 4.5, 1 -> -1, 0.03 -> 0.5, 9 -> a}

The desired output is {2 -> 3, 1 -> -1}.

I have tried the following, to no avail:

Cases[List1, _Integer -> _Integer]

Cases[List1, IntegerQ[#[[1]]] && IntegerQ[#[[2]]] &]

Cases[List1, Rule[Integer_, Integer_]]

How can I obtain the desired output?

$\endgroup$

3 Answers 3

6
$\begingroup$

You can name the pattern:

Cases[List1, r : (_Integer -> _Integer)]

{2 -> 3, 1 -> -1}

You can also wrap the pattern with PatternSequence

Cases[List1, PatternSequence[_Integer -> _Integer]]

{2 -> 3, 1 -> -1}

This also works:

Cases[List1, _[__Integer]]

{2 -> 3, 1 -> -1}

$\endgroup$
6
$\begingroup$

Use HoldPattern:

 Cases[List1, HoldPattern[_Integer -> _Integer]]

{2 -> 3, 1 -> -1}

$\endgroup$
5
$\begingroup$

You could use Verbatim

Cases[List1, Verbatim[Rule][_Integer, _Integer]]

{2 -> 3, 1 -> -1}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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