Python Forum
Compare elements of tuples / Find edges between nodes - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Compare elements of tuples / Find edges between nodes (/thread-25620.html)



Compare elements of tuples / Find edges between nodes - Daniel94 - Apr-05-2020

[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]


RE: Compare elements of tuples / Find edges between nodes - deanhystad - Apr-06-2020

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).