MapThread over sublists of different length
You can use Flatten
to transpose a ragged array:
list = {{1, 0, 3, 4}, {0, 2}, {0, 0, 1, 3}, {1, 2, 0}}
Count[#, 0] & /@ Flatten[list, {{2}, {1}}]
(* {2, 2, 1, 0} *)
Edit
Step one is to transpose your list but in this case the list is ragged so Tranpose
doesn't work:
Transpose[list]
However Flatten
can transpose a ragged list (type Flatten
in the documentation center and then go to "Applications"):
Flatten[list, {{2}, {1}}]
(* {{1, 0, 0, 1}, {0, 2, 0, 2}, {3, 1, 0}, {4, 3}} *)
Now that the list is transposed you can count the number of zeros, this is done by mapping the transposed list onto Count
Map[Count[#, 0] &, Flatten[list, {{2}, {1}}]]
I propose this:
Total @ PadRight[1 - Unitize[list]]
Say you have
l = {{1, 0, 3, 4}, {0, 2}, {0, 0, 1, 3}, {1, 2, 0}, {1, 1, 1, 1, 0}};
There is a bit different approach to your function:
Sort@Tally@Position[l, 0][[All, 2]]
{{1, 2}, {2, 2}, {3, 1}, {5, 1}}
which is compressed from of your information. It gives you slot index and number of 0s there. If slot has no zeros it is not mentioned. If you have a lot of zero-less slots such format is much shorter.
Grid[{{"slot", "zeros"}}~Join~%, Frame -> All]
And here is clunky exercise in padding arrays (with help of Mike's comment) :
zcount[l_List] := With[{m = Max[Length /@ l]}, (Count[#, 0] & /@
Transpose[PadRight[#, m, None] & /@ l])]
The usage:
zcount[l]
{2, 2, 1, 0, 1}