Hello. What are some ways that one can work with (x,y) coordinate pairs in Python? I'm doing a homework assignment that requires me to create a program that can check for certain binary relations (reflexive, transitive, symmetric), and I'm having an issue coming up with the best approach for it. I'm trying to use lists, but coming up with the best logic to work with them is the big issue here. Here's the code I have so far:

x = []
y = []
def get_relation():
    choice = "YES"
    while (choice.upper() != "NO"):
        x-coord = int(input("Enter the x-coordinate: "))
        y-coord = int(input("Enter the x-coordinate: "))
        choice = input("Would you like to enter another coordinate?: ")

def IsReflexive():
    reflex_x= []
    reflex_y= []
    for i in range(len(x)):
        if (i+1 < len(x)):
            if (x[i+1] != x[i]):
    for i in range(len(reflex_x)):
    for i in range (len(x)):

         if (reflex_x[i] == 

The main issue I'm having is that, in the reflexive function I'm building, I'm not sure how to go about making sure that I only get reflexive coordinates in the form (a,a), i.e., (1,1), (2,2), and not have the program give me issues with always saying something is reflexive due to the way I'm performing the searches. Is there a more efficient way to do this that isn't so intensive on the logic? I wanted to try my hand at dictionaries, but I don't want to assume that there are no duplicate pairs (the assignment didn't specify, so I don't want to get tripped up on that). I'm more than likely also gonna experience the same issue with the search logic for the other two functions. What can I do?

4 Years
Discussion Span
Last Post by greatman05

Your suggestion worked for all of the functions I created; thank you very much!

Here is the final source:

Relation = [] #Relation list
def get_relation(): #Gets the input relation
    choice = "YES" #Choice variable
    print("Getting input relation; type 'Yes' to enter more coordinates"
          " or 'No' to stop.")
    while (choice.upper() != "NO"): #Checks to see if the user input 'No'; if so, stops the loops
        x_coord = int(input("Enter the x-coordinate: ")) #Get x-coordinate
        while (x_coord == ""):#Make sure coordinate was input
            x_coord = int(input("Enter the x-coordinate: "))
        y_coord = int(input("Enter the y-coordinate: ")) #Get y-coordinate
        while (y_coord == ""): #Make sure y-coordinate was input
            y_coord = int(input("Enter the x-coordinate: "))       
        Relation.append((x_coord,y_coord)) #Add a tuple containing x and y coordinate to list
        choice = input("Would you like to enter another coordinate?: ") #Allow user to enter more coordinate pairs or end the loop

def print_relation(relation): #Prints relation
    for i in range(len(relation)):

def duplicates(list_, thing): #Checks to see if there is a duplicate object in a list
    if(list_ == []): #Needed for empty relations
        return False
        for object_ in list_: #Checks to see if a duplicate exists; if it doesn't, returns False
            if thing in list_:
                return True #Otherwise, it returns True
                return False

def find_thing(list_, thing): #Checks to see if something is in a list
    for object_ in list_:
        if thing in list_: #If it is, return True
            return True
        else: #Otherwise, return false
            return False

def wait(): #End of program function to pause program and allow user to view output
    print("Press any key to continue.")

def IsReflexive(relation): #Checks to see if a relation is reflexive
    x_list = [] #Value list
    reflexive_list = [] #Reflexive value list
    reflexive = 0 #Reflexive counter
    for thing in relation: #For everything in the relation, check to see if the x or y value already exists in the x-value list
        if (duplicates(x_list,thing[0]) == False): #If it doesn't, add it to the list
            x_list.append(thing[0]) #The 'thing' is actually a tuple, and tuples are very useful for these kind of comparisons
        if (duplicates(x_list,thing[1]) == False):
    for thing in x_list: #Append the reflexive tuple comparisons to a reflexive list
    for thing in reflexive_list: #If we find an object from the reflexive list in the relation, add 1 to the reflexive counter
        if (find_thing(relation,thing) == True):
            reflexive += 1
    if (reflexive == len(reflexive_list)): #If all objects in the reflexive list were found, print a success message
        print("The relation is reflexive.")
    else: #Otherwise, print a failure message
        print("The relation is not reflexive.")

def IsSymmetric(relation): #Checks whether or not a relation is symmetric
    symmetric = 0 #Symmetric counter
    for thing in relation: #For every oject in the relation, if the object and its reflection are found, add 1 to the symmetric counter
        if (find_thing(relation,thing) == True): #i.e if (a,b) and (b,a) are found, +1 to the symmetric counter
            if (find_thing(relation, (thing[1],thing[0])) == True):
                symmetric +=1
    if (symmetric == len(relation)): #If all objects were found to be symmetric, print a success message
        print("The relation is symmetric.")
    else: #Otherwise, print a failure message
        print("The relation is not symmetric.")

def IsTransitive(relation): #Checks to see if a relation is transitive
    transitive = 0 #Transitive counter
    for i in range(len(relation)): #Creates two thing objects that represent two coordinates to be compared
        if (i < len(relation) - 1): 
            thing = relation[i]
            thing2 = relation[i+1]
        elif (i == len(relation)): #Allows us to compare the first and the last objects in the relation after all others have been compared
            thing = relation[0]
            thing = relation[len(relation)-1] 
        if(find_thing(relation,(thing[0],thing[1])) == True): #Check to see if the following is true: (a,b), (b,c), (a,c) are all elements of the relation
            if(find_thing(relation, (thing[1],thing2[0])) == True): #If all conditions are true, transitive counter increments by 1
                if(find_thing(relation, (thing[0],thing2[1])) == True):
                    transitive += 1
    if (transitive == len(relation)): #If all objects were found to be transitive, print a success message
        print("The relation is transitive.")
    else: #Otherwise, print a failure message
        print("The relation is not transitive.")

get_relation() #Get the user's relation
print("This is the entered relation: ") #Output the user's relation
IsReflexive(Relation) #Check to see if the relation is reflexive
IsSymmetric(Relation) #Check to see if the relation is symmetric
IsTransitive(Relation) #Check to see if the relation is transitive
wait() #Allow user to view output
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.