Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a list which looks like this: data={{12,0},{0,0},{20,1},{0,0}, {0,-1}}. Now I would like to count the number of sublists which:

  • have both slots zero
  • the first slot zero and the second not
  • the second slot zero and the first not
  • the first slot < 10 and any the second zero
share|improve this question
4  
Not impossible ;-) but have you tried something on your own yet? Things like Select, Cases and Count come to mind here. – Yves Klett Sep 13 '12 at 7:10
The results are: 2, 1, 9 and 2. – F'x Sep 13 '12 at 7:31

1 Answer

up vote 3 down vote accepted

Using Cases and Count:

{Count[data, {0, 0}], Count[data, {0, _}], 
  Count[data, {_, 0}], 
  Count[data, {0, Except[0]}], 
  Count[data, {Except[0], 0}], 
  Count[data, {a_, 0} /; a < 10]}
  (* {2, 3, 3, 1, 1, 2} *)

 {Cases[data, {0, 0}], 
  Cases[data, {0, _}], 
  Cases[data, {_, 0}], 
  Cases[data, {0, Except[0]}], 
  Cases[data, {Except[0], 0}], 
  Cases[data, {a_, 0} /; a < 10]}
 (* {{{0, 0}, {0, 0}}, 
    {{0, 0}, {0, 0}, {0, -1}}, 
    {{12, 0}, {0, 0}, {0,  0}}, 
    {{0, -1}}, 
    {{12, 0}}, 
    {{0, 0}, {0, 0}}}*)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.