Search code examples
pythonfor-loopmathcoordinatesnested-loops

Nested for loop on tupled coordinates


I need to write a code that will eventually read in coordinates from across multiple tables of big data stored in excel sheets but first I wanted to learn to write a nested for loop to analyze the tuples in the code below.

All I can find for nested for loops don't have anything like this so I thought it could be good to post up here.

What I need this code to do specifically is to take the first coordinate in file1 and compare it to each coordinate in file2, Then second coordinate in file1 to each coordinate in file2, and so on to loop through each coordinate in file1 compared to every coordinate in file to and then return if the two share the specified proximity.

import math
file1 = ('1.36, 7.11', '1.38, 7.12', '1.5, -7.14', '8.7, 3.33', '8.5, 3.34', '8.63, -3.36')
file2 = ('1.46, 7.31', '1.47, 7.32', '1.49, -7.34', '8.56, 3.13', '8.57, 3.14', '8.59, -3.16')
dist = file1.apply(lambda row: math.hypot(row['x_diff'], row['y_diff']), axis=1)
for dist in file1:
    for dist in file2:
        if dist.values >= .5:
            print 'no match'
        elif dist.values <= .5:
            print True, dist

My hunch with whats wrong is that I am not filling the appropriate command to read the tuples as coordinates. Then furthermore, I am having a lot of confusion as to what I ought to write in this statement here for dist in file1. By that I mean what I am supposed to call and how to label it appropriately.

I realize this is probably a mess but, this is my first coding project ever so if absolutely anybody can help steer me in the right direction or provide some feedback as to what I might need to understand better here I would greatly appreciate it.


Solution

  • You represent your tuples as strings which is very inconvenient to work with. "Real" tuples are generally better to start off.

    file1 = [(1.36, 7.11), (1.38, 7.12), (1.5, -7.14), (8.7, 3.33)]
    file2 = [(1.46, 7.31), (1.47, 7.32), (1.49, -7.34), (8.56, 3.13)]
    

    The next question is, how do we get the distance between those two xy points? For this, we can use scipy.spatial.distance.euclidean as a function which takes two tuples and returns the euclidean norm of the vector in between. For example:

    > import scipy.spatial.distance as distance
    > distance.euclidean(file1[0], file2[0])
    0.22360679774997827
    

    Now, we come to core of your question: the nested loop. The logic is as follows. For each element in file1, say coord1, we take each element in file2, say coord2 and compute the distance between coord1 and coord2.

    for coord1 in file1:
        for coord2 in file2:
            dist = distance.euclidean(coord1, coord2) # do not forget to import distance before
            if dist < 0.5:
                print True, dist
            else:
                print 'no match'
    

    I would name the variables after what they represent. file1 is a coordinate_list (first_coordinate_list, coordinate_list_1) and the elements are coordinates, e.g. coordinate, coordinate_1, left_coordinate.