Keep only rules that replace integers with other integers

4

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?

am111

Posted 2018-06-05T00:23:50.923

Reputation: 43

Answers

6

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}

kglr

Posted 2018-06-05T00:23:50.923

Reputation: 302 076

6

Use HoldPattern:

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

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

Carl Woll

Posted 2018-06-05T00:23:50.923

Reputation: 112 778

5

You could use Verbatim

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

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

Coolwater

Posted 2018-06-05T00:23:50.923

Reputation: 17 733