Sorry for for the potentially silly question. But this seems to be a stumping problem I just can't find the answer to.
a = [((1,1),(0,0)), (3,4)]
I'd like to check if the following tuples
d appear in
b = (1,1) c = (0,0) d = (3,4) print(b in a) # <- False ? print(c in a) # <- False ? print(d in a) # <- True
I'd like to replace the code in each print statement in such away that the search finds the tuple in the list and as such returns
Any help at all would be much appreciated. Apologies if this question has already been asked before.
We need a recursive function that takes a list or tuple and an element to find.
Each function should check whether the element is in the current iterable using the
in operator. If it is in then return
True, otherwise check if it is in any of the lower dimensions by recursively calling itself with each of the lower iterables and return if any of those succeeded (this can be done with a
for-loop, but we may as well use the
So a one liner would be roughly:
def inc(it, e): return True if e in it else any(inc(iit, e) for iit in it if type(iit) in (list, tuple))
And it works as intended:
>>> inc(a, (1,1)) True >>> inc(a, (0,0)) True >>> inc(a, (3,4)) True