Nested for loops


#1

Hi,
I am trying to check if an item in list 1 is present in list 2 and return a list of values based on items in list 3.
Example:

List1: [zero, two, three, four, five, seven]
List2: [one, zero, five, three, six]
List3: [0,1,4,7,9]

The returned list should be: [1,0,7,0,4,0]

My code is listed below, but my returned list is wrong. Can anyone find my error?

Update: I have changed the code to simplify it, but I still don’t end up with a useful result:


#2

I think this might be a bug with the ternary operator inside imperative code:

try an if statement.


#3

@tmdA5HML You can achieve that in Python as shown below:

List1=IN[0]
List2=IN[1]
List3= IN[2]
OUT=[]
for item1 in List1:
	if item1 in List2:
		ind=List2.index(item1)
		OUT.append(List3[ind])	
	else:
		OUT.append(0)

#4

In if statement codeblock form:

t1 = List.IndexOf(y,x);
t1==-1?0:List.GetItemAtIndex(z,t1);

#5

Thanks @salvatoredragotta ! This worked perfectly.


#6

I tried an if statement earlier with the same result, but using Python worked. It seemed the for loop was the problem.

Thanks for your help, though!


#7

@tmdA5HML please mark the post as solved