Hi all,
I’ve 2 (nested) lists. I want to compare the booleans at the same index number. I want to filter out 3 options
1: true / false
2: false / true
3: true / true
If I combine the both lists in an and condition, I got ‘null’. So I guess I’ve to work with a function.
Anyone an idea how to solve this properly ? Much appreciated!
KR Merlijn
Hey,
So, yes I couldn’t compare a list with an == either, so I cheated! I’d be interested to see it done properly…
Hopefully it’s useful in the meantime.
Mark
1 Like
If you want false/false to return true for onward boolean filtering, and everything else to be false, then alternatively you could use one line of Python
OUT = [not (i or j) for i, j in zip(IN[0][0], IN[0][1])]
Hope this helps,
Thomas
2 Likes
You could also transpose the list and then use a List.Contains to check for true
.
It would technically give you the opposite of @Thomas_Corrie’s answer (instead of false,false
being true
, it is a false
) but should still work the same.
Edit: Better way is to just use List.AllFalse, will give you true when both are false:
2 Likes
So we can treat a String as an object and check it’s constituent parts… It’s a list of characters?
But are we concluding that we can’t treat a list as an object in the same way? I’m slightly surprised by that I’d be interested to understand the coding reason…
I’m lost, what string are you talking about?
Apologies,
So in my example, I made a string from the true and false… I could test that string object is == to another string containing ‘truefalse’
But seemingly we can’t test a list to see if it is == to another list composed of the same items (true, false)
I’m surprised that a ‘list of characters’ (string) is different to a list of booleans? If that makes sense?
Hi Kennyb,
thanks for your response. I understand your reasoning, but the order also matter, so true / false or false true, is also important and should lead to a different outcome, do you understand? Now you’re simply checking if one of the values is true.
If I’m not clear enough let me know!
Thanks anyway
Okay, so its not that you want to filter out the 3 combinations of non-falses, but that you want to separate all 4 variations?
very creative Mark! This explains quite well what I need! But I agree with you that there should be amore logical option to fix it.
1 Like
Thanks @Thomas_Corrie, I’ll try that and then also for the other combinations.
How about this? The python returns which variation it is and from there you can group, sort, etc. the original data.
Python below:
booldict = {
"[True, True]":"A",
"[True, False]":"B",
"[False, True]":"C",
"[False, False]":"D"
}
OUT = [booldict[str([i,j])] for i,j in zip(IN[0][0], IN[0][1])]
2 Likes
Hi @kennyb6,
thanks a lot, this does the trick. Now just for me to understand python better, can I also do this with a for loop?
Can you give me some hints what I’m doing wrong? And I get the message unexpected token ‘i’, but I don’t see what I’m doing wrong here…
Booleans in Python are capitalized, so True instead of true etc
TBH I would just use Kenny’s code and adjust the inputs to be a dictionary.
Also the code is only getting the first index of each list… EDIT: Sorry I misread the node
Worth noting, that as with mine, he’s converting to Strings to work with a dictionary, then he’s converting the inputs to strings.
Cheers,
Mark
1 Like
Once the booleans are corrected, I think it iterates fine over the zipped lists? @merlijnvanleeuwen the problem with not having a grouping for scenario D (false/false) as put forward by Kenny in his solution is that the indexing loses its relationship with the original list which might cause you issues downstream, so I would recommend having that
1 Like
Are you just checking two different booleans for four different options?
4 Likes
yeah thanks! Oke, I though I needed to end the loop with else, but elif does the trick.
so perhaps another question if its not asked too much
I’d like to use the filter by boolean mask now, so this is working fine, but just that I learn better how to use python I’d like to rebuild the filter by boolean mask in python.
So here is how I’ve done it with filter by boolean mask, but can anyone help me how to do this in python?
this is how I’m thinking, how do I get the item of this list (with the same index number of where the loop is at that moment and output.append the column itself instead of the letter A,B,C or D.
Is this somehow clear?
@Nick_Boyts; yes exactly! wauw that looks simple
can you explain how you’ve done that? How should I read this ‘first?(argument) :(argument)’? I didnt know this code block…
In this case it’s actually one If statement nested within another, but it’s just design script shorthand.
condition ? ifTrue : ifFalse
In your case the booleans completely replace the condition because… well… True == True
.
1 Like