Python Mentors,
I can't get this code to run and at times, I can't even save it. It is
sections of code used from previous exercises, put it together and it just
isn't right.
Thank-you,
LN
The method is as follows:
1. Run the distance calculations for pt1 to all other points, and print
or save the results.
2. Set a holding variable for pt1 values, then switch the values from
pt1 to pt2 (so pt2 is now in the first position of your coordinate list).
3. Calculate the new distances for pt2 to all other points, and print
or save the results.
4. Switch pt2 and pt1 so that they are back in their original
positions.
5. Re-set the holding variable for pt1 values, then switch the values
from pt1 to pt3 (so pt3 is now in the first position of your coordinate list).
Calculate the new distances for pt3 to all other points, and print or save the
results.
… continue with this method until all points are done.
Here is the text file of points:
ID X Y
PT1 2.6 8.7
PT2 5.6 10.3
PT3 8.9 45.7
PT4 10.4 6.2
PT5 2.1 21.4
PT6 8.7 78.2
PT7 44.5 15.2
PT8 23.6 45.8
PT9 43.1 2.3
PT10 1.1 62.5
# Description: read in all of the given data file and then calculate
# the distance between each of the data points and then write or print out the
results
# to a new text file.
# Open path to file, readlines and create variables, run a 'while' loop, split
the line (initialize with import string) into the three variable lists
infile = open("z:/filepath/coordinate.txt","r")
line = infile.readline()
import math
import string
IDCODE = []
XCOORDINATE = []
YCOORDINATE = []
n = 0
while True:
line = infile.readline()
if not line: break
ID,X,Y = string.split(line)
XNUM = float(X)
YNUM = float(Y)
n = n + 1
XCOORDINATE.append(XNUM)
YCOORDINATE.append(YNUM)
IDCODE.append(ID)
print (XCOORDINATE, YCOORDINATE), IDCODE
#
# activate the math modular function (use import math function) in order to use
the
#square root function
dist = {}
def distance(n,p,s, XCOORDINATE, YCOORDINATE, IDCODE):
import math
p = 1
s = 0
DP = []
while p < n:
DT= math.sqrt((XCOORDINATE[p]-XCOORDINATE[0])**2 +
(YCOORDINATE[p]-YCOORDINATE[0])**2)
DP.append(DT)
p = p + 1
while s < n-1:
dist[DP[s]] = IDCOORDINATE[s+1]
s = s + 1
for key in sorted(dist):
print dist[key],[key]
return dist
def switch(n,XCOORDINATE, YCOORDINATE, IDCODE):
import math, string
idcodezero = IDCODE[0]
xcodezero = XCOORDINATE[0]
ycodezero = YCOORDINATE[0]
z = 1
while z <=n - 1:
IDCODE[0] = IDCODE[z]
XCOORDINATE[0] = XCOORDINATE[z]
YCOORDINATE[0] = YCOORDINATE[z]
IDCODE[z] = IDCODEzero
XCOORDINATE[z] = XCOORDINATEzero
YCOORDINATE[z] = YCOORDINATEzero
DR = distance(n,XCOORDINATE, YCOORDINATE, IDCODE)
IDCODE[z] = IDCODE[0]
XCOORDINATE[z] = XCOORDINATE[0]
YCOORDINATE[z] = YCOORDINATE[0]
IDCODE[0] = IDCODEzero
XCOORDINATE[0] = XCOORDINATEzero
YCOORDINATE[0] = YCOORDINATEzero
DP = []
z = z + 1
#
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor