1
$\begingroup$

From the following 3d-Table

t1={{{1, 1}, {2, 1}, {3, 4/3}}, {{1, 0}, {2, 1/2}, {3, 1}}, {{1, -1}, {2,
0}, {3, 2/3}}}

I want to use Select for getting all pairs where the second Part is an Integer. So that I would get

{{{1, 1}, {2, 1}}, {{1, 0}, {3, 1}}, {{1, -1}, {2, 0}}}

I can do it by

Pick[t1, Map[IntegerQ /@ #[[All, 2]] &, t1]]

But there should be more elegant ways to do it. How to use Select or Select with Function or some other twist?

$\endgroup$
2
  • 2
    $\begingroup$ Select[IntegerQ@#[[2]] &]/@t1 or Cases[{_, _Integer, ___}] /@ t1? $\endgroup$
    – kglr
    Aug 8 '20 at 14:55
  • $\begingroup$ Great! Compact and easy! $\endgroup$
    – user57467
    Aug 8 '20 at 15:08
2
$\begingroup$
Select[IntegerQ@#[[2]] &] /@ t1

Cases[{_, _Integer, ___}] /@ t1

DeleteCases[t1, {_, Except[_Integer], ___}, {2}]

Pick[t1, t1[[All, All, 2]], _Integer]

all give

{{{1, 1}, {2, 1}}, {{1, 0}, {3, 1}}, {{1, -1}, {2, 0}}}

If the sublists in the innermost layer are pairs of numbers (as in the example in OP) you can also use

Map[Select[IntegerQ@*Last]]@t1
Cases[{_, _Integer}] /@ t1
DeleteCases[t1, {_, Except[_Integer]}, {2}]
$\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.