[Tutor] import values, calculate distance

Joel Goldstick joel.goldstick at gmail.com
Thu Aug 21 15:13:09 CEST 2014


On Wed, Aug 20, 2014 at 10:12 PM, LN A-go-go
<lnartist at yahoo.com.dmarc.invalid> wrote:
> 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:
>
> Run the distance calculations for pt1 to all other points, and print or save
> the results.
> 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).
> Calculate the new distances for pt2 to all other points, and print or save
> the results.
> Switch pt2 and pt1 so that they are back in their original positions.
> 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

For ease of reading, put your imports at the top of your file
the code below should probably be put in a function.  Its purpose is
to read your data
and populate your lists.
>From here ----------------------------------------------------------------------------------------------------------
> IDCODE = []
> XCOORDINATE = []
> YCOORDINATE = []
while you can name things like this, good python style (look up
'pep8') says this would be better
id_code, x_coordinate, y_coordinate

But better yet, I think would be to call these things:
id, x, y

> n = 0
> while True:
>  line = infile.readline()
>  if not line: break
>  ID,X,Y = string.split(line)

The only use of X, and Y are to use as placeholder so that you can
convert to float
ID serves no purpose since you copy it to IDCODE below
temp = string.split(line)
x = float(temp[1])
y = float(temp[2])
id_code.append[temp[3])
>  XNUM = float(X)
>  YNUM = float(Y)


>  n = n + 1

>  XCOORDINATE.append(XNUM)
>  YCOORDINATE.append(YNUM)
>  IDCODE.append(ID)
>
> print (XCOORDINATE, YCOORDINATE), IDCODE
to here ----------------------------------------------------------------------------------------------------------------

This looks like a new function below
> #
> # 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 at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
Joel Goldstick
http://joelgoldstick.com


More information about the Tutor mailing list