1
$\begingroup$

If I have some list of lists of coordinates, i.e.

list={{{0,1},{1,2},{2,3},{3,4}},{{3,2},{2,3},{1,4},{0,5}},{{3,1},{2,2},{2,4},{3,5}}}

how can I count how many of the y coordinates in each of the lists has a value of 2 or greater? E.g. the first list has 3 y coordinates greater than or equal to 2, the second has 4 and the third has 3. I have tried

list1=list[[All, All, 2]]
Count[Map[list1], u_ /; u > 2]

but this isn't correctly separating my data by list.

$\endgroup$
  • 1
    $\begingroup$ Try this: Count[#, x_ /; x[[2]] >= 2, 1] & /@ list. $\endgroup$ – Anjan Kumar Mar 15 '18 at 15:44
  • $\begingroup$ That's great thanks! $\endgroup$ – JJH Mar 15 '18 at 15:52
2
$\begingroup$

Here's one way:

Total[UnitStep[list[[All,All,2]]-2],{2}]

{3, 4, 3}

$\endgroup$
1
$\begingroup$

Here is another version using Count (another one is given in a comment):

Count[{_, _?(GreaterEqualThan[2])}] /@ list

And another one that I also like:

Count[{_, y_ /; y >= 2}] /@ list

Putting the condition close to the variable makes it clear what is happening.

$\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.