0

I have my input file,

Node ID    X    Y
   1       5   10
   2       8   20 
   3       9    5
   4       7   10

Where first column shows the Node ID, while second and third columns shows the X and Y co-ordinates respectively. How to find the distance between one another node ?

Tanay Suthar
  • 453
  • 3
  • 8
  • 19
  • Is it a Python Question?: if it is then most probability duplicated of [How can the euclidean distance be calculated with numpy?](http://stackoverflow.com/questions/1401712/how-can-the-euclidean-distance-be-calculated-with-numpy) – Grijesh Chauhan Feb 20 '15 at 18:38
  • 1
    Do you have tried some thing already, and encounter with any problem? – Mazdak Feb 20 '15 at 18:39
  • Search for `with open() as...`, the formula for the distance, `split()`, try some code, and feel free to ask for any problem you encounter with your code. – user Feb 20 '15 at 18:43
  • Here for the first node, co-ordinates would be (5,10) and for the second node (8,20) now from the Euclidean distance formula, d = sqrt((8-5)^2 + (20-10)^2). But I need to produce each of the node's distance with another – Tanay Suthar Feb 20 '15 at 18:43
  • ...additionally include the clarification from your comment inside the question, so that readers know that it's not a 3 dimension point. – user Feb 20 '15 at 18:45
  • I modified my problem – Tanay Suthar Feb 20 '15 at 18:51

2 Answers2

1

first create dictionary crd

crd = {}

Then you should read your file using readlines. As in my case I use your original text, I used string function split to break it into lines (althought I believe ther must be much better ways to do this)

for line in a.split('\n'):
    m = map(int,line.split())
    crd[m[0]] = m[1:]

crd
{1: [5, 10], 2: [8, 20], 3: [9, 5], 4: [3, 10]}

Finally lest compute the distance

from math import sqrt
dist = lambda d,x,y: sqrt((d[x][0]-d[y][0])**2 + (d[x][1]-d[y][1])**2)

dist(crd,1,2)
10.44030650891055

update: if you need all combinations I suggest

for i in sorted(crd.keys()):
    for j in sorted(crd.keys()):
        if j>i:
            print i,j,':',dist(crd,i,j)

1 2 : 10.4403065089
1 3 : 6.40312423743
1 4 : 2.0
2 3 : 15.0332963784
2 4 : 11.1803398875
3 4 : 7.81024967591
Juraj Bezručka
  • 512
  • 5
  • 21
0

First read the data into a list of lists then call a function.

with open("file.txt","r") as f:
    a = [map(int,l.split()) for l in f if l.split()]
    dist(a)
a    
[[1, 5, 10], [2, 8, 20], [3, 9, 5], [4, 3, 10]]    

then by using the itertools combinations() function, you get all the node pairs.

def dist(data):
    comb = list(combinations([b[0] for b in data],2))
comb
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]

Then is a matter of iterate through the list and get the points for input

    for (x,y) in comb:
        x1 = data[x-1][1] 
        x2 = data[y-1][1]
        y1 = data[x-1][2] 
        y2 = data[y-1][2]
        d = sqrt((x2-x1)**2 + (y2-y1)**2)
        print "{}-{} dis = {}".format(x,y,d) 

Which outputs:

1-2 dis = 10.4403065089
1-3 dis = 6.40312423743
1-4 dis = 2.0
2-3 dis = 15.0332963784
2-4 dis = 11.1803398875
3-4 dis = 7.81024967591
Daniel
  • 5,095
  • 5
  • 35
  • 48