Increse value based on occurances

Hi everyone,

I am going to show the exercise that I am trying to replicate in Dynamo but with a screenshot from Excel.

As you can see below, I have a column that shows TRUE or FALSE. FALSE is always gonna be “0” but I would like to achieve that when it starts the list, all first consecutive TRUE values are going to be “1”. Then, if a FALSE is in between and the TRUE values appear again, those TRUE values shall be “2” instead of “1”.

In conclusion, what I need is to increase in 1 value the latest TRUE ID used.

Thank you so much!

I’d probably write a loop in Python.

Starting with a count of 1 …
Then a check to see if you’d hit True then False and if you have increase the count by 1.

Output 0 for all your falses and output count for the trues

yeh, I thought about Python but my knowledge is quite limited tough…

What if there’s a false first?
Does the first true read 1 or 2?

If it wants to read 2 then:

BUT if you always want your first True to read 1 then:

True_False = IN[0]

state = True
Trues = 0
count = 1
out =

for i in True_False:
if i == False:
out.append(0)
state = False
elif i == True:
Trues +=1
if state == False:
if Trues !=1 :
count +=1
state = True
out.append(count)

OUT = out

Here is a Design Script approach.
You could attempt to create the same with nodes.

//Seperate consecutive true and false
cns01 = List.DropItems(lst01,1) == List.DropItems(lst01,-1);
cns02 = List.Flatten([-1, List.AllIndicesOf(cns01,false),List.Count(lst01)-1], -1);
cns03 = List.DropItems(cns02,1) - List.DropItems(cns02,-1);
lst02 = List.Chop(lst01,cns03);

// Assign value to true
lst03 = List.Flatten(List.UniqueItems(lst02<1>));
lst04 = Math.Sum(List.TakeItems(lst03 == true ? 1 : 0, 1..List.Count(lst03)));
lst02 == true ? lst04 : 0;

sqnNmb.dyn (9.0 KB)

1 Like

Hi @Alien ,

Thank you so much for the support.

I tried to create the node in Dynamo but I had to flatten the sublists in order to have the same structure as you have. Despite flattening the sublists, I am getting an Empty list outcome from the Python node:

Would it be possible to make the Python node work with sublists?

Thank-you so much.

Not sure what you mean. It works fine for the flattened list?

Have you got the square brackets on line 6 ?

Now it is working fine. As you can see in the screenshot highlighted in red, my input is a string so I had to write in the Python node the “true” and “false” between quotes.

Then, I highlighted in green how my input is. You will notice that I have several levels (sublists) instead of a flattened list (highlighted in green).

You just nest it… if everything’s on the same level

Right… I am running away from my computer now :laughing:

But it does not keep the same list structure as you can see in the following image:

Is it possible to keep the same sublists? I am so sorry for my unknowledge in Python :sweat:

Quick solution… use the list chop or try the designscript that Vikram posted

Hi @Vikram_Subbaiah ,

First of all, thank you so much for your supoprt. I did not see that you posted.

Please find a screenshot with a sample of how the script should work. You will notice that it must work with sublists and it increases the values within a sublist and then it starts again for the next sublist.

That would require a slightly altered approach

def numTruSqn (lst01:var[]..[])
{
	//Seperate consecutive true and false
	cns01 = List.DropItems(lst01,1) == List.DropItems(lst01,-1);
	cns02 = List.Flatten([-1, List.AllIndicesOf(cns01,false),List.Count(lst01)-1], -1);
	cns03 = List.DropItems(cns02,1) - List.DropItems(cns02,-1);
	lst02 = List.Chop(lst01,cns03);

	// Assign value to true
	lst03 = List.Flatten(List.UniqueItems(lst02<1>));
	lst04 = Math.Sum(List.TakeItems(lst03 == true ? 1 : 0, 1..List.Count(lst03)));
	return lst02 == true ? lst04 : 0;
};

For a flattened list you call the function with
numTruSqn(lst01);

With sublists
numTruSqn(lst01<1>); or numTruSqn(lst01@L1);

Nested further (as in your sample)
numTruSqn(lst01<1><2>); or numTruSqn(lst01@L2);

sqnNmb.dyn (12.3 KB)

Thank you so much for your quick response.

I opened your script and replaced the data type of the inputs (inmy script are string insteadof booleans) and I am not getting the desired outcome:

If I also tried the same in my own Dynamo script, I have same outcome:

false on line 5 needs to remain a bool

def numTruSqn (lst01:var[]..[])
{
	//Seperate consecutive true and false
	cns01 = List.DropItems(lst01,1) == List.DropItems(lst01,-1);
	cns02 = List.Flatten([-1, List.AllIndicesOf(cns01,false),List.Count(lst01)-1], -1);
	cns03 = List.DropItems(cns02,1) - List.DropItems(cns02,-1);
	lst02 = List.Chop(lst01,cns03);

	// Assign value to true
	lst03 = List.Flatten(List.UniqueItems(lst02<1>));
	lst04 = Math.Sum(List.TakeItems(lst03 == "true" ? 1 : 0, 1..List.Count(lst03)));
	return lst02 == "true" ? lst04 : 0;
};

sqnNmb.dyn (12.3 KB)

1 Like

Man… you are great! Thank-you so much for your support. I wish I could have your skills :frowning:

1 Like