我想在 LAT、LNG 的坐标对中计算“曼哈顿距离”,也称为“城市街区距离”。
在这篇文章曼哈顿距离的两个地理位置之后,我使用haversine公式计算了距离:
source = (45.070060, 7.663708)
target = (45.068250, 7.663492)
Run Code Online (Sandbox Code Playgroud)
这是我的计算:
from math import radians, sin, asin, sqrt, atan2
# convert decimal degrees to radians
lat1, lon1, lat2, lon2 = map(radians, [source[0], source[1], target[0], target[1]])
#haversine formula for delta_lat
dlat = lat2 - lat1
a = sin(dlat / 2) ** 2
c = 2 * atan2(sqrt(a), sqrt(1-a)))
r = 6371
lat_d = c * r
# haversine formula for delta_lon
dlon = lon2 - lon1
a …Run Code Online (Sandbox Code Playgroud) 我有一个名为matrix包含一些行的列表.每个row字典都包含一些字典,每个字典可以包含在多行中.
我想生成一个列表dictionaries,其中包含矩阵中的所有字典,但没有重复.我已经有了解决方案,但我想使用理解.
row1 = [{'NODE':1}, {'NODE':2}, {'NODE':3}]
row2 = [{'NODE':3}, {'NODE':4}, {'NODE':5}]
row3 = [{'NODE':4}, {'NODE':6}, {'NODE':7}]
matrix = [row1, row2, row3]
dictionaries = []
for row in matrix:
for dictionary in row:
items.append(dictionary) if dictionary not in dictionaries else None
print dictionaries
[{'NODE':1}, {'NODE':2}, {'NODE':3}, {'NODE':4}, {'NODE':5}, {'NODE':6}, {'NODE':7}]
Run Code Online (Sandbox Code Playgroud)
我想要像下面这样的东西,但它不起作用,因为我在创建它时不能要求检查列表:
dictionaries = [dictionary for row in matrix for dictionary in row if dictionary not in dictionaries]
Run Code Online (Sandbox Code Playgroud)
字典键和值是原始的不可变对象,如字符串和整数.
python dictionary list-comprehension duplicates nested-lists
python ×2
coordinates ×1
dictionary ×1
distance ×1
duplicates ×1
geolocation ×1
haversine ×1
nested-lists ×1