Creating a switch in outputs

I’m trying to generate an output based on a list of strings, which keeps on giving the same output until it says otherwise, at which point it’ll change around for the rest of the list.
In this case I’m trying to pick out r’s and l’s and switch on the basis of that.

So in this case the output is
1ro
2l
3
4b
6o
7
8r

…and with that I’d like the output to be

r
l
l
l
l
l
l
r

I hope I’m making myself clear.

This is what I got so far:


I have already filtered out the positions of the inputs and got the indices of the l’s and the r’s but I can’t really think of a way to generate the list that I’d like to have.

Thanks in advance!

This is in python if it is okay with you:

Codeblock:
String.Contains(x,"r",true) ? "r" : String.Contains(x,"l",true) ? "l" : null;

Python:

strlist = IN[0]
outcome = []
for i,v in enumerate(strlist):
	if v != None:
		outcome.append(v)
	else:
		outcome.append(outcome[i-1])

OUT = outcome

Another example:


You will get an index error if the first item is a null though. It can be done in only the python node with a little change of code but the codeblock lets you easily choose what to look for/replace with and can see where it should be repeating.

1 Like

Thanks a lot!

I should really start to learn python properly.

I’m already using and altering python scripts effectively but to build them up from scratch. Knowing loops a little makes these things apiece of cake but I just don’t really know how to build them up.

The same thing can be done in an imperative codeblock but I am just really bad at them :joy: There might be methods using ootb nodes but for loops are the easiest for me to think of.

Really just learning for loops, if statements, and the occasional while loop is majority of python. If you need help remembering the syntax, google and stackoverflow (very helpful) are good to search in. I have to do it often too.

1 Like

I come across a problem like this every couple of weeks and each time I feel like facepalming myself because I still haven’t gotten around to learning this.

I already learned that trying to loop within the dynamo code is generally a massive annoyance, so I’ll just avoid that.

1 Like