Python with mulitple outs

Guys,

Help me out here. (New with python).

I made a small script which checks a list for certain characters. And I’d like to output them seperately.

Input is a list of names which starts with 31. … example
31.01
31.A01
31.15
31.16
31.H05

_Output is _
IF filterz is applicable:
1, the [3]th character (A.H, K or S)
2. the 4th and 5th (01, 05)
ELSE
1. the 3th and 4th character (01, 15,16)

    OUT=[]
filterz = ['31.A', '31.H', '31.K','31.S']
for i in IN[0]:
	if any(n in i for n in filterz):
		OUT.append(i[3])
		OUT.append(i[4:6])
	else:
		OUT.append(i[3:5])

Now I get one big list, which isn’t helpful.

I’d like the list
OUT1: ["", A,"","",H]
OUT2: [01,01,15,16,05]

OR even better sublists?

OR am I better off creating two scripts and combine those afterwards?

From python you will get one output. The easiest way to solve your problem is to organize your filter in lists and Output them as a list.
With this method you will have a list with three lists as an output. Then you just need to add another code block node that takes each list, like this:

lst[0];
lst[1];
lst[2];

(If you have 3 main filters).

If this works, and if you know you’ll need this process very often, you can also create a simple custom node with all the outputs needed.
Tell me if I’ve been clear!

Andrea

So those lst’s are in fact ‘sublists’ of OUT?

if so, how can I do that?

out[0].append(i[3])
out[1].append(i[4:6])
doesn’t work …

test1=[]
    test2=[]
    OUT=[test1, test2]
    filterz = ['31.A', '31.H', '31.K','31.S']
    for i in IN[0]:
    	if any(n in i for n in filterz):
    		test1.append(i[3])
    		test2.append(i[4:6])
    	else:		
    		test1.append("")
    		test2.append(i[3:5])
  1. This the way to do it?
  2. Empty is done through “” ?

It’s very easy actually, but I didn’t understand your syntax. Try to use a cleaner code, your indentation was incorrect. Hope is clearer now, if not, give us your input list and I’ll write a filter for you, but try by yourself first!

Thanks,

The codeblock “lst[0]” is nifty (I was using the chop, and flatten nodes, lolz).

@andrea.nicosiavinci
you did good! Explaining an issue is often harder than the actual solution, sorry for that.

input list is anything starting with “31.”.
And I have to make sure only
31.xx (x= [0-9])
31.yxx (y= [A,H,K,S]), (x=[0-9])
are used.
and therefore ideally 31.biscuits is ignored [to do list :slight_smile:] .

P.CODE

list1, list2=[], []
filterz = ['31.A', '31.H', '31.K','31.S']
for i in IN[0]:
	if any(n in i for n in filterz):
		list1.append(i[3])
		list2.append(i[4:6])
	else:		
		list1.append('')
		list2.append(i[3:5])
OUT=list1, list2
1 Like

Hello @3Pinter! Maybe a regular expression would be easier in your case?

Here is a possible pattern:

31\.[0-9,A,H,K,S][0-9]+

2 Likes

Yes, in this case is far better! I assumed @3Pinter was trying to learn python, we already have lot of packages for filtering. Great node, by the way, Clockwork is always very smooth!

@andrea.nicosiavinci I am! Understanding how things work, makes creating things through nodes / own scripts a lot easier.

@Einar_Raknes interesting, I will check that one. Was fiddling on https://regex101.com/ for that one :slight_smile: