5
$\begingroup$

I have a program that generates a list of specific 3-tuples of integers. A typical output is as follows:

list = {{0, 0, 2}, {0, 1, 1}, {0, 2, 0}, {1, 0, 1}, {1, 1, 0}, {2, 0, 0}}

I want to remove all 3-tuples {x_,y_,z_} where $x$ or $y$ (or both) are odd. The output in the example should be

cleanlist = {{0,0,2},{0,2,0},{2,0,0}}

Note that 1 isn't the only odd integer that can appear.

I tried using something like cleanlist = DeleteCases[DeleteCases[list, {_OddQ,y_,z_}],{x_,_OddQ,z_}], but unfortunately OddQ doesn't behave like Integer.

Any help is appreciated

$\endgroup$
  • 6
    $\begingroup$ Try DeleteCases[list, {_?OddQ, _, _} | {_, _?OddQ, _}] and report back. $\endgroup$ – J. M.'s technical difficulties May 27 at 18:59
  • $\begingroup$ It works, thanks! $\endgroup$ – johnny May 27 at 19:02
6
$\begingroup$
Cases[{__?EvenQ, _}] @ list
{{0, 0, 2}, {0, 2, 0}, {2, 0, 0}}
DeleteCases[Except @ {__?EvenQ, _}] @ list
{{0, 0, 2}, {0, 2, 0}, {2, 0, 0}}
Select[Apply[And] @* EvenQ @* Most] @ list
{{0, 0, 2}, {0, 2, 0}, {2, 0, 0}}
GroupBy[Total[Mod[ #, 2]] &][list][0]
 {{0, 0, 2}, {0, 2, 0}, {2, 0, 0}}
| improve this answer | |
$\endgroup$
5
$\begingroup$
Pick[list, Mod[list[[All, 1 ;; 2]], 2].{1, 1}, 0]
| 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.