0

I have this code:

def closest():
	citiescomp=[]
	distances=[]
	for i in range(0,len(city_names)-1):
	    citiescomp.append([city_names[i], citiesx[i], citiesy[i]])
	while True:
		try:
			targetx=int(raw_input('Please enter the x coordinate of the city you wish to target: '))
			if targetx in range(0,1000):
				break
			else:
				print "Oops, it seems like you made a mistake. Try again by entering a number between 0 and 1000."
		except ValueError:
			print "Oops, it seems like you made a mistake. Try again by entering a number between 0 and 1000"
	while True:
		try:
			targety=int(raw_input('Please enter the y coordinate of the city you wish to target: '))
			if targety in range(0,1000):
				break
			else:
				print "Oops, it seems like you made a mistake. Try again by entering a number between 0 and 1000."
		except ValueError:
			print "Oops, it seems like you made a mistake. Try again by entering a number between 0 and 1000"
	for i in range(0, len(citiescomp)):
		distance = (sqrt( (targetx - int(citiescomp[i][1]))**2 + (targety - int(citiescomp[i][2]))**2 ))
		distance=round(distance, 2)
		distances.append(distance)
	shortest_distance=min(distances)
    closest_city=distances.index(shortest_distance)
    print "-"+citynames[closest_city+count],"at", round(shortest_distance,3),"units away"

citiescomp looks like this; citiescomp=[['city1', '111', '222'], ['city2', '333', '444'], ['city3', '555', '666'], ['city4', '777', '888']]

What happens is I find which city is the closest to the target city, but I want to sort them so that they are in order of shortest to farthest and print all distances while keeping the same name that was given to the coordinates of the city. "

Thanks,


Jaro

Editor's note:
You are mixing spaces and tabs in your indentations, a very bad habit that will make your code unusable for other people. Please use spaces only, the common standard is 4 spaces.

Edited by vegaseat: tabs are bad

3
Contributors
3
Replies
5
Views
5 Years
Discussion Span
Last Post by Gribouillis
Featured Replies
  • Using the [icode]key[/icode] argument of sorted() or list.sort(), you can sort a list of items according to a score function. The score of an item could be its price, name, age, mark, length, distance to a point, etc depending on what the items are. [code=python] from functools import partial def … Read More

  • [QUOTE=Ene Uran;1584524]Oh boy, Jaro's tab indented code looks like dung on a shingle.[/QUOTE] An old trick to reindent your code is to use Tim Peters' reindent script [url]http://pypi.python.org/pypi/Reindent/0.1.0[/url] . Read More

2

Using the key argument of sorted() or list.sort(), you can sort a list of items according to a score function. The score of an item could be its price, name, age, mark, length, distance to a point, etc depending on what the items are.

from functools import partial

def dist_to_target(tx, ty, item):
    name, sx, sy = item
    return sqrt((int(sx) - tx)**2 + (int(sy) - ty)**2)

score = partial(dist_to_target, targetx, targety)
# score is now a function score(item) --> distance to target

sorted_items = sorted(citiescomp, key = score)
print sorted_items

Edited by Gribouillis: n/a

Comments
nice code
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.