Message Boards Message Boards

[✓] Count the zeros between the ones in a list?

GROUPS:

I have a list with zero’s separated by ones and I want to count the zero’s between the ones. I tried pure functions but in the wrong way. Can anybody give me a solution? (Needless to say that I am not very experience in Mathematica.)

In[23]:= Test = {1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0};

In[24]:= Test = Flatten[Position[Test, 1]]

Out[24]= {1, 6, 9}

{* The output should be {4, 2} I tried Map[#2 - #1 - 1 &, Test] but it dosn' t.

POSTED BY: Chiel Geeraert
Answer
13 days ago

You may try SequenceSplit:

l = {1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0};
SequenceSplit[l, {1 ..}]
Map[Length, %]

This counts also the trailing three zeros. You can correct that problem if you really need it.

POSTED BY: Gianluca Gorni
Answer
13 days ago

Thank you very much. In the meantime I have found a simple , not very sophisticated, workaround solution but it works.

I will try your suggestion and try to understand it.

Best regards, Chiel Geeraert

POSTED BY: Chiel Geeraert
Answer
12 days ago
t=FromDigits[Test]//IntegerString;
StringCases[t,x:("1"~~"0"..~~"1"):>StringLength[x]-2,Overlaps->True]

Maybe faster for large list?

POSTED BY: Raspi Rascal
Answer
13 days ago

Can be done even shorter:

SequenceCases[{1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0}, x : {0 ..} :> Length[x]]
POSTED BY: Sander Huisman
Answer
13 days ago

Thank you very much. In the meantime I have found a simple , not very sophisticated, workaround solution but it works.

I will try your suggestion and try to understand it.

Met vriendelijke groet, Chiel Geeraert

POSTED BY: Chiel Geeraert
Answer
12 days ago
SequenceCases[{1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0}, x : {1,0 ..,1} :> Length[x]-2,Overlaps->True]
POSTED BY: Raspi Rascal
Answer
13 days ago

Thank you very much. In the meantime I have found a simple , not very sophisticated, workaround solution but it works.

I will try your suggestion and try to understand it.

Best regards, Chiel Geeraert

POSTED BY: Chiel Geeraert
Answer
12 days ago

We would like to see your workaround solution.

POSTED BY: Raspi Rascal
Answer
12 days ago

Here is my workaround :

In[20]:= Test = {1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0,
    0, 0, 0, 0, 1, 0, 0};
(* The desired output is the number of zero's between the ones so
{4,2,5,7} *)

In[15]:= Test = Flatten[Position[Test, 1]]

Out[15]= {1, 6, 9, 15, 23}

In[17]:= Result = 
 Table[Test[[k + 1]] - Test[[k]] - 1, {k, 1, Length[Test] - 1}]

Out[17]= {4, 2, 5, 7}
POSTED BY: Chiel Geeraert
Answer
12 days ago

A slight variation:

Differences@Flatten@Position[Test, 1] - 1
POSTED BY: Hans Milton
Answer
11 days ago

Really a nice solution. I learned quite a few things from all reactions.

Best regards, Chiel Geeraert (Netherlands)

POSTED BY: Chiel Geeraert
Answer
11 days ago

Mr. Rascal,

Your idea of converting the list to a string is a very nice solution . (SequenceCases doesn't work with my version of Mathematica). I understand how it works and I keep in my trickbox.

Best Regards , Chiel Geeraert

POSTED BY: Chiel Geeraert
Answer
12 days ago

Chiel, your code is good. But it returns too many unneeded values, try for example with this list:

Test = {0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
    0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0};

SequenceCases[ ] was introduced in 2015 (10.1) and is like Cases[ ] terribly inefficient. If your list has over 3 000 elements, then you can forget about the solution with SequenceCases[ ]. The solution with StringCases[ ] will work superfast even with lists with over 1 000 000 elements. I've tested it with test lists:

SeedRandom[0];
Test = RandomChoice[{0.3, 0.7} -> {1, 0}, 1000000];

The trick to convert a regular list into a string is not a trick but a common thing to do when dealing with huge lists plus a pattern matcher.

POSTED BY: Raspi Rascal
Answer
12 days ago

To the collection of possible approaches here comes one more:

data = {0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0};
ComponentMeasurements[Image[1 - {data}], "Count"]
POSTED BY: Henrik Schachner
Answer
10 days ago

Well, I tried out your solution it it works fine. But to be honest : it's a bit out of reach of my Mathematica knowledge. I will give it thorough look with the help-function of Mathematica.

Best regards, Chiel Geeraert

POSTED BY: Chiel Geeraert
Answer
10 days ago

Group Abstract Group Abstract