Imagine a function:
def Intersects (x, list1, list2, list3):
if x in list1 and x in list2 and x in list3: return True
return False
There has to be a better way to do this but I can’t figure it out. How can I do this? (performance is important)
EDIT: I’ve come across a related, however harder problem this time. This time I’ve got 3 unrelated integers and I need to test if they intersect too.
Like:
1, 2, 3 <-- elements to look for
if InAll ((1, 2, 3)) ...
but I am not looking for a tuple, instead I am looking for just integers. How do I unpack the tuple and do the same search?
If you want to be able to give it any number of lists (not just three), you might like:
If you are checking memberships in these lists many times (on many
xs, that is), you’ll want to make each into a set before you start looping through thexs.From your most recent edit, it looks like you also want to be able to pass multiple items in
x. Code for that would be: