计算列表中坐标之间的距离

时间:2019-04-16 03:08:14

标签: python-3.x

myDict={"pizzaHut" : ["PizzaHut", [155, 407], [1100, 2200], "halal", "fast-food", [10.00, 30.00], "Hawaiian Ala-Carte ($10.80)"],
"starbucks" : ["Starbucks Coffee", [155, 407], [700, 2200], "halal", "beverages", [6.00, 11.00], "Vanilla Latte ($6.70)"],
"subway" : ["Subway", [155, 407], [800, 2100], "halal", "fast-food", [5.00, 10.00], "Chicken Teriyaki Sandwich ($6.70)"]}

x = input("What is the x-coordinate of your location?")
y = input("What is the y-coordinate of your location?")

i want to find the distance of the different eateries from the location input by the user, how do i do that? Really new to python so i dont know how to start

datalist = list(myDict.values())

destinationList = []

for i in datalist:
    destinationList.append([i[0],i[1]])

distance = []
def distance (x, y,destinationList):
    for element in destinationList:
        x1 = element[1][0]
        y1 = element[1][1]
    distance.append((((x1-x)**2)+((y1-y)**2))**0.5)
print(distance)

但这不起作用。 输出应该是从3个餐厅计算出的3个用户位置距离的列表

1 个答案:

答案 0 :(得分:-1)

完整代码

myDict={"pizzaHut" : ["PizzaHut", [155, 407], [1100, 2200], "halal", "fast-food", [10.00, 30.00], "Hawaiian Ala-Carte ($10.80)"],
"starbucks" : ["Starbucks Coffee", [155, 407], [700, 2200], "halal", "beverages", [6.00, 11.00], "Vanilla Latte ($6.70)"],
"subway" : ["Subway", [155, 407], [800, 2100], "halal", "fast-food", [5.00, 10.00], "Chicken Teriyaki Sandwich ($6.70)"]}

x = int(input("What is the x-coordinate of your location?\n"))
y = int(input("What is the y-coordinate of your location?\n"))


datalist = list(myDict.values())

destinationList = []

for i in datalist:
    destinationList.append([i[0],i[1]])

distance = []
def find_distance(x, y,destinationList):
    for element in destinationList:
        x1 = element[1][0]
        y1 = element[1][1]
        distance.append((((x1-x)**2)+((y1-y)**2))**0.5)
    print(distance)

find_distance(x,y,destinationList)