Take the 2-minute tour ×
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
add comment

1 Answer

up vote 6 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
    
fantastic explaination! –  Mika Ike yesterday
add comment

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.