Python Forum
Compare elements of tuples / Find edges between nodes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compare elements of tuples / Find edges between nodes
#1
[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]
Reply
#2
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).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to find difference between elements in a list? Using beginner Basics only. Anklebiter 8 4,254 Nov-19-2020, 07:43 PM
Last Post: Anklebiter
  DFS with weighted edges livbat 2 4,972 May-08-2019, 07:30 PM
Last Post: livbat
  Recursive Function - Compare 2 lists, return the elements that don't exist in both KellyBaptist 1 5,179 Dec-23-2018, 10:10 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020