python - 从循环更新值时,字典似乎有点偏离和缺失值
问题描述
所以我有一个区域列表和一个目标列表,我想要做的就是使用haversine获得离该目标最近的区域和距离它的距离。我遇到的问题是,当我得到最终的字典时,它并没有把所有的记录都放在里面,我怀疑有些东西没有被正确捕获,这使得顺序混乱,但很难弄清楚它在哪里。
这是我的代码,如果不是很清楚,请告诉我
#Main dictionary with results
center_dict = dict()
for index, row in targets.iterrows():
#latitude and longitude columns for the targets
p_lat = row[9]
p_lon = row[10]
#name of the target, to be primary key of dict
name = row[0]
distances = dict()
#loop of the areas to map against
for ind, r in pins.iterrows():
#longitude and latitude data for those areas
r_lat = r[2]
r_lon = r[3]
#calling haversine function that returns a distance in miles
distance = haversine(p_lon, p_lat, r_lon, r_lat)
#adding distance to the distances dict with the location name as a key
distances[r[0]] = distance
#taking minimum value from dict and the key for this
closest = min(distances, key=distances.get)
closest_value = min(distances.values())
#adding the two above to the list as well as a unique id for them which is in the data
center_dict[name] = {row[1], closest, closest_value}
解决方案
推荐阅读
- python - numba jit 是否有可能减慢我的 gcd 执行速度?
- javascript - 如何使用 js 在特定日期过期 XML 节点?
- laravel - 获取所有相关记录 laravel
- angularjs - Angularjs $http服务jsonp方法不断返回404状态码
- android - 将 EditText 文本保存到数据库的性能问题
- python - 修改关闭某个变量的函数时出现问题。旧的封口不适合。空闭包崩溃解释器
- c# - Azure Function v2 中的 BrokeredMessage 发送和消息使用者
- angularjs - AngularJS 中的 $$passToExceptionHandler 不抛出异常
- python - 使用列表列表访问字典中的值并返回列表列表
- bluetooth-gatt - 连接OBD设备时,setCharacteristicNotification和writeCharacteristic,onCharacteristicChanges收到3E 00 00 3F通知