4
$\begingroup$

At here How can I select elements that are true

list={{{2, 4, -3}, {3, 5, -1}}, {{2, 4, -3}, {3, 5, 7}}, {{2, 4, -3}, {5, 
   1, 9}}, {{2, 4, 9}, {3, 5, -1}}, {{2, 4, 9}, {3, 5, 7}}, {{2, 4, 
   9}, {5, 1, -3}}, {{3, 2, -4}, {5, 1, -3}}, {{3, 2, -4}, {5, 1, 
   9}}, {{3, 2, -4}, {5, 4, 6}}, {{3, 2, 10}, {5, 1, -3}}, {{3, 2, 
   10}, {5, 1, 9}}, {{3, 2, 10}, {5, 4, 6}}, {{3, 5, -1}, {6, 2, 
   7}}, {{3, 5, -1}, {7, 2, 4}}, {{3, 5, 7}, {6, 2, -1}}, {{3, 6, 
   2}, {5, 1, -3}}, {{3, 6, 2}, {5, 1, 9}}, {{3, 6, 4}, {5, 
   1, -3}}, {{3, 6, 4}, {5, 1, 9}}, {{5, 1, -3}, {6, 2, -1}}, {{5, 
   1, -3}, {6, 2, 7}}, {{5, 1, -3}, {7, 2, 4}}, {{5, 1, 9}, {6, 
   2, -1}}, {{5, 1, 9}, {6, 2, 7}}, {{5, 1, 9}, {7, 2, 4}}}

I want to Select from list an element {pA,pB}so that Mod[pA + pB, 2] == {0, 0, 0}. I tried

Table[{pA, pB} = points;
  {pA, pB, Mod[pA + pB, 2] == {0, 0, 0}}, {points, list}];
Cases[list2, {__, True}] 

I got

{{{3, 2, -4}, {5, 4, 6}, True}, {{3, 2, 10}, {5, 4, 6}, True}}

Can I reduce my code and can I remove word True to get,

{{{3, 2, -4}, {5, 4, 6}}, {{3, 2, 10}, {5, 4, 6}}}

$\endgroup$
  • 4
    $\begingroup$ As you yourself say, use Select: Select[list, Mod[#[[1]] + #[[2]], 2] == {0, 0, 0} &] $\endgroup$ – swish Mar 9 '18 at 13:00
  • 3
    $\begingroup$ A variation of @swish's solution uses the selection function Mod[Total[#], 2] == {0, 0, 0} &. $\endgroup$ – J. M. is in limbo Mar 9 '18 at 13:02
5
$\begingroup$

Pick is typically faster than Select. On large lists, I would recommend

Pick[list, Total[Mod[Total[list, {2}], 2], {2}], 0]

which is 10 or 20 times faster than these variations.

Select[list, Mod[Total[#], 2] == {0, 0, 0} &]

Cases[list, _?(Total[Mod[Total[#], 2]] == 0 &)]
$\endgroup$
4
$\begingroup$

You can use Nothing:

{{{3, 2, -4}, {5, 4, 6}, True}, {{3, 2, 10}, {5, 4, 6}, True}} /. True -> Nothing

(* {{{3, 2, -4}, {5, 4, 6}}, {{3, 2, 10}, {5, 4, 6}}} *)
$\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.