Justin T
Justin T

Reputation: 47

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.

Upvotes: 0

Views: 1067

Answers (3)

Rafael Albert
Rafael Albert

Reputation: 445

For-loops in general :
You choose one variable as an iterator (can be anything, but shouldn't be something that is used elsewhere at the same time) which iterates through an iterable (e.g. a list). In my example below, i and j are the iterators while range(10) is the object they iterate through. Within the loop, you write everything down you want to have repeated. In my example below, I append every possible i/j combination to the list.

Nested for loops require you to use two different variables.

Example:

whatever = []
for j in range(10):
    for i in range(10):
        whatever.append([j, i])

After running the code, whatever will look like that:

[ [0, 0], [0, 1], [0,2] ,... [1, 0], [1, 1], ... [9, 9] ]

Upvotes: 2

Michael Hoff
Michael Hoff

Reputation: 6318

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.

Upvotes: 1

dashiell
dashiell

Reputation: 812

Assuming you're getting your data in as tuples:

# convert file1 and file2 to lists of 2d points
# this is quite sloppy and I'll tidy it up when I get home from work
xs = [[float(pq.split(',')[0]),float(pq.split(',')[1])] for pq in list(file1)]
ys = [[float(pq.split(',')[0]),float(pq.split(',')[1])] for pq in list(file2)]
# generate a cartesian product of the two lists
cp = [(x,y) for x in xs for y in ys]
# generate distances
dists = map(lambda (x,y):math.hypot(x[0]-y[0],x[1]-y[1]),cp)
# loop through and find distances below some_threshold
for i in range(len(xs)):
    for j in range(1,len(ys)+1):
        if dists[i*j] > some_threshold:
            print i,j,dist
        else:
            print 'no match'

I would recommend using pandas or numpy if you're going to be reading in any reasonably sized dataset, however.

Upvotes: 1

Related Questions