gpt4 book ai didi

python - 在 Python 中浏览字典

转载 作者:太空宇宙 更新时间:2023-11-03 15:59:41 25 4
gpt4 key购买 nike

我有一本名为“位置”的字典。我为字典编写的函数以(d,描述,当前)格式设置,其中“d”引用字典,“描述”引用描述我们正在查找的位置的字符串,“当前”引用发现我们当前在字典中是一对坐标(x,y)。

基本上,每个位置在字典中都有多个位置,每个位置都有自己的坐标对,我的目标是找到距离我们当前在字典中的位置(当前)最近的位置。策略是使用距离公式来计算。

例如,如果我们正在寻找最近的加油站并且当前位于 (2,2),则如果两个加油站分别位于 (3,1) 和 (2,2),则该函数应返回 (3,1) 作为最近的加油站(1,4) 因为 (3,1) 更接近 (2,2) 任何有关我当前代码的建议将不胜感激。

代码:

def closest(d, description, current):
current_location = (x, y)
d = {(3,1):'gas', (1,4):'gas', (2,1):'food', (5,5):'food'}
distancesFromCurrent = [distanceFormula(z, current_location) for z in places]
for z in d:
if z < minimum float:
return z

我当前的代码没有错误,但肯定无法正常工作。它只是返回 0,0,我不确定如何修复它以返回距离我们当前位置最近的位置的坐标。

最佳答案

考虑了评论后,这是我的解决方案。

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 6 21:42:22 2016

@author: michaelcurrin
"""

import math

def findDistance(A, B):
"""
In 2D space find the distance between two co-orinates is
known as Eucliciean distance.
Args
A: tuple or list of x and y co-ordinates
e.g. (1,2) e.g. [1,2]
B: as A.
Retuns
distance: float. Decimal value for shortest between A and B
"""
x = (A[0] - B[0])
y = (A[1] - B[1])
distance = math.sqrt(x**2 + y**2) # square root

# remove comment if you want to see this outputted
# print distance

return distance


def GetClosestPlace(places, loc, feature):
"""find shortest distance between current location and each locations
but only ones which have the desired feature"""

# add distance from current location to each location
for index in range(len(places)):

# only continue if feature exists at place
if feature in places[index]['features']:

# calculate
distance = findDistance(loc,
places[index]['location'])
else:
# this is to represent n/a for now as every location needs a distance
# for this version, so that it will not be chosen
distance = 1000

# add calculated distance to existing dictionary for location
places[index]['distance'] = distance


# find shortest distance and return details for that place

allDistances = [x['distance'] for x in places]
shortestDistance = min(allDistances)

for place in places:
if place['distance'] == shortestDistance:
return place


placesList = [dict(name='foo',location=(0,3), features=['gas', 'food']),
dict(name='bar',location=(4,6), features=['food', 'hospital']),
dict(name='abc',location=(0,9), features=['gas','barber']),
dict(name='xyz',location=(2,2), features=['food','barber'])
]

currentLocation = (5,9)
desiredFeature='food'

closestPlace = GetClosestPlace(placesList, currentLocation, desiredFeature)

print 'Current location: %s' % str(currentLocation)
print 'Desired feature: %s ' % desiredFeature
print
print 'The closest place is...'
print 'Name: %s' % closestPlace['name']
print 'Location %s' % str(closestPlace['location'])
print 'Distance %f' % closestPlace['distance']
# join multiple features in the list with commas
print 'Features: %s' % ', '.join(closestPlace['features'])

"""
OUTPUT

Current location: (5, 9)
Desired feature: food

The closest place is...
Name: bar
Location (4, 6)
Distance 3.162278
Features: food, hospital
"""

关于python - 在 Python 中浏览字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40452887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com