6
$\begingroup$

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
$\endgroup$
0

1 Answer 1

9
$\begingroup$

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}}}*)
$\endgroup$
1
  • $\begingroup$ fantastic explaination! $\endgroup$
    – Mika Ike
    Apr 19, 2014 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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