# Measuring multiple incidences of a continuous constant in a list

I had a hard time trying to word this question properly; I apologize...

So I have a (very) big list of 0's and 1's:

   AllTimes = {{0}, {0}, {0}, {1}, {1}, ...}


I want to somehow measure the number of continuous 1's (i.e., if the list is {{0}, {1}, {1}, {0}, {1}, {0}} I want to get the values 2 and 1). Is there a way of running a LengthWhile as a loop to give me not just one, but all lengths of continuous 1's?

I've also (miserably) failed to program a different method:

For[i = 1, i <= 100000, i++, t = 0
If[AllTimes[[{i}]] = 1,
t = t + 1]
If[AllTimes[[{i}]] = 0, Print[t], t = 0]]


I wanted each index of AllTimes to be read, and if it's 1, a value of t is added. If the value of an index gives 0, I want the t to be printed and reset back to 0. (I'm totally new to Mathematica programming... so please be gentle...). Although instead of printing, is there a way to also store the value of t before it is reseted to 0?

• Welcome to Mathematica SE, I am sure someone will be able to give a good, short solution to the problem you've tried to describe in your post. Dec 3, 2015 at 10:22
• What about: Length /@ SequenceCases[l, {{1} ..}]? Dec 3, 2015 at 10:24
• Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign!
– user9660
Dec 3, 2015 at 10:59

list = {{0}, {1}, {1}, {0}, {1}, {0}};

Length /@ Cases[Split@Flatten@list, {1 ...}]


{2, 1}

• sorry I edited your answer by mistake - I rolled back. Dec 3, 2015 at 10:28

A rather new function (10.1) here:

l = {{0}, {1}, {1}, {0}, {1}, {0}};

Length /@ SequenceCases[l, {{1} ..}]


(* {2, 1} *)

Code:

Length@# & /@ Cases[Split@Flatten[#, 1], {1 ...}] &[{{1}, {1}, {0}, {1}}]


Output:

{2,1}

Reference:
Length
@ | /@ / #
Split
Flatten
Cases

• Thanks for the references! It's really nice to understand how each command is used :) Dec 3, 2015 at 10:47
• You are very welcome! There is lots of useful information here on Mathematica SE.I would encourage you to visit the most up-voted question section; two posts in particular: "Where can I find examples of good Mathematica programming practices?" && "What are the most common pitfalls awaiting new users?" to get yourself up-to-speed with Mathematica. Dec 3, 2015 at 11:00

Using SequenceSplit (new in 11.3)

list = {{0}, {1}, {1}, {0}, {1}, {0}};

Length /@ SequenceSplit[list, {{0}}]


{2, 1}

Since @eldo and @E. Chan-López revived this thread a very small contribution to their efforts.

list = {{0}, {1}, {1}, {0}, {1}, {0}};


then

Length /@ Select[Split[Flatten[list]], Total[#] > 0 &]


{2, 1}

Using SplitBy and DeleteCases:

l = {{0}, {1}, {1}, {0}, {1}, {0}};

Length /@ DeleteCases[SplitBy[l, {{1}}], {{0}}]

(*{2, 1}*)


Another way is to use ConsecutiveQ to split by positions as follows:

Length@Extract[l, #] & /@ SequenceCases[Position[l, {1}], {__}?ConsecutiveQ]

(*{2, 1}*)