I have this code:

import csv
from math import *
distances=[]
pos=0
tally=0
target_x=input("Please enter the X coordinate of the target city: ")
target_y=input("Please enter the Y coordinate of the target city: ")
count = sum(1 for row in csv.reader( open('cities.csv') ) )
cityname = csv.reader(open("cities.csv", "rb"))
citynames = []
citynames.extend(cityname)
cityname = []
for data in citynames:
    citynames.append(data[0])
    tally+=1
    if tally>=count:
        break
tally=0
#x coordinate list
citiesx = csv.reader(open("cities.csv", "rb"))
cities_list_x = []
cities_list_x.extend(citiesx)
cities_x = []
for data in cities_list_x:
    cities_x.append(int(data[1]))
    tally+=1
    if tally>=count:
        break
tally=0
#y coordinate list
citiesy = csv.reader(open("cities.csv", "rb"))
cities_list_y = []
cities_list_y.extend(citiesy)
cities_y = []
for data in cities_list_y:
    cities_y.append(int(data[2]))
    tally+=1
    if tally>=count:
        break
tally=0
while pos<count:
        distance = (sqrt( (target_x - cities_x[pos])**2 + (target_y - cities_y[pos])**2 ))
        distances.append(distance)
        pos+=1
for i in range(0,count):
    shortest_distance=min(distances)
    closest_city=distances.index(shortest_distance)
    print str(i+1)+"-"+citynames[closest_city+count],"at", round(shortest_distance,3),"units away"
    del distances[distances.index(shortest_distance)]
raw_input("Press enter to exit")

When I use this spreadsheet;

city1 433 566
city2 677 788
city3 899 900

and input 678 for the first input and 789 as the second input I get this as output:

Please enter the X coordinate of the target city: 678
Please enter the Y coordinate of the target city: 789
1-city2 at 1.414 units away
2-city2 at 247.31 units away
3-city1 at 331.291 units away
Press enter to exit

When I think it should be:
"Please enter the X coordinate of the target city: 678
Please enter the Y coordinate of the target city: 789
1-city2 at 1.414 units away
2-city3 at 247.31 units away
3-city1 at 331.291 units away
Press enter to exit"

Any help is great

Thanks,


Jaro

Recommended Answers

All 4 Replies

i instead of count at line 48 maybe?

i instead of count at line 48 maybe?

Your code is really messed up, for what purpose you are doing it, own project or school?

I would write it completely again from begining, as you have long code, but even then it does not do rudimentary checks. I get:

Please enter the X coordinate of the target city: 678
Please enter the Y coordinate of the target city: 789

Traceback (most recent call last):
  File "K:/test/dt1.py", line 25, in <module>
    cities_x.append(int(data[1]))
IndexError: list index out of range

Probably because you do not pass the empty line at end of file, if file ends properly with '\n'

So basically you want to count distance to three cordinates from given cordinate and say which was closest and how far:

from math import hypot

cities = (('a', (433, 566)),
          ('b', (677, 788)),
          ('c', (899, 900))
          )

my_x, my_y = (678, 789)
distances = [(name, hypot(city_x - my_x, city_y - my_y))
             for name, (city_x, city_y) in cities]
distances.sort(key=lambda (x,y): y)
print('\n'.join('Distance to %r is %8.3f units' % (city, d)
                for city, d in distances))
commented: good suggestion +15

Your problem is that once you remove the shortest distance from your distances array, when you search for the next-smallest distance, the index matches your cities list only if it comes before the previous one. If it comes after, you're looking at the wrong city. I think you can fix the problem by deleting the cityname at the same time as the distance -- since distances.index(shortest_distance) is already assigned to closest_city , try:

...
    del distances[closest_city]
    del citynames[closest_city]

You don't need "+count" when you print your cityname, except that your (inadvertently?) extended onto the end of the same array at the top of your program (for x and y, you use different array-names). Note: all that reading code is still completely ugly, but at least this should get your program working as expected.

How is it going, you have still not marked this thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.