Python Forum

Full Version: Compare elements of tuples / Find edges between nodes
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
[Image: result.png]

I want to check if any of the tuples my list contain the specific variables “x” and “y”.
If tuple 1 contain “x”,”x” and tuple 2 contain “y”, “y” return should be false.
But if tuple 3 contain “x”,”y,” return should be true.
Also I don’t want the last element of any tuple to take part in the comparison.


def check(lst, x, y):
    return any(x in tpl and y in tpl for tpl in (x[:-1] for x in lst))
I am using that comparison function. However the problem is this:
* Added nodes: "a" and "b"
* Added edges: (a,b) and (b,b)

However the edge (a,a) comes back as true when the tuple ('a','a',1) don't exist.
[Image: error.png]
Your logic is checking if x and y are both in the tuple. if x=a and y=a, both are in the tuple(a, b).