6
$\begingroup$

Here is the code I tried but failed to achieve what I want.

list={{{-0.678629, -0.247568}, {-0.555433, -0.21052}}, {{0.606745, \
-0.147331}, {-0.246826, 0.426555}}, {{-0.040916, 
   0.329272}, {-0.261357, -0.164607}}, {{0.883787, 
   0.432163}, {-0.555896, 
   0.690923}}, {{-0.00193167, -0.00326776}, {-0.155916, 
   0.0141922}}, {{0.393342, -0.0505961}, {-0.24225, -0.780803}}, \
{{0.194168, 0.196396}, {0.455803, -0.158632}}, {{-0.416166, 
   0.596879}, {0.0341831, 0.000531598}}, {{0.222941, 
   0.0134066}, {-0.107692, -0.59851}}, {{0.390906, -0.24567}, \
{0.146006, 0.393449}}};
 list//Select[#, (#[[#]][[2]]) >= 0 &] &

I want pick an element that every 2nd part of this element at level 1 is greater than 0.

For instance, {{-0.416166, 0.596879}, {0.0341831, 0.000531598}} is suitable because both 0.596879 and 0.000531598 are greater than 0.

$\endgroup$
4
$\begingroup$
Select[And @@ NonNegative[#[[All, 2]]] &] @ list
{{{0.883787, 0.432163}, {-0.555896, 0.690923}}, 
 {{-0.416166, 0.596879}, {0.0341831, 0.000531598}}}

Also

Select[NonNegative[Min@#[[All, 2]]] &] @ list

Cases[{{_, _?NonNegative, ___} ..}] @ list

Pick[list, And @@@ NonNegative[list[[All, All, 2]]]]

Pick[list, NonNegative[Min /@ list[[All, All, 2]]]]
$\endgroup$
2
  • 2
    $\begingroup$ How about those elements which equal to 0? In my code above, it's >=0. $\endgroup$ – kile Jul 13 '20 at 5:41
  • 1
    $\begingroup$ @kile, i updated with the correction (NonNegative instead of Positive). $\endgroup$ – kglr Jul 13 '20 at 5:45
4
$\begingroup$

I think this is what you may be after:

Select[list, #[[1, 2]] > 0 && #[[2, 2]] > 0 &]
{{{0.883787, 0.432163}, {-0.555896, 0.690923}}, 
 {{-0.416166, 0.596879}, {0.0341831, 0.000531598}}}
$\endgroup$
1
  • $\begingroup$ Thanks, it works. But is there a more efficient way to do this? #[[1, 2]] > 0 && #[[2, 2]] > 0 is obviously a little long. It's acceptable when every element has 2 parts but It's extremely bulky when every element has 4 parts. $\endgroup$ – kile Jul 13 '20 at 4:58
3
$\begingroup$

Another way:

Select[list, AllTrue[Last /* GreaterEqualThan[0]]]

{{{0.883787, 0.432163}, {-0.555896, 0.690923}}, {{-0.416166, 0.596879}, {0.0341831, 0.000531598}}}

$\endgroup$
2

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.