Python中2d列表的迭代,两个位置之间的距离

2024-06-26 14:14:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个2d列表(列表列表),其中我有几个在美国的地点,以及它们相关的纬度和经度。以下是前几个城市的一个例子:

"Millersville","CA",35.303850,-118.457860    #(city 1)
"Millersville","IL",39.451150,-89.158140     #(city 2)
"Millersville","IN",39.853100,‐86.091650     #(city 3)
"Millersville","MD",39.059550,-76.648020     #(city 4)

为了节省城市和地点之间的距离,我需要计算出城市和地点之间的距离,然后用1和locu的距离来计算每个值。以下是我正在修补的内容,但我无法让它通过迭代:

^{pr2}$

我做错了什么?如何让这个迭代设置遍历我所有的城市(未知数量取决于我使用的列表),并计算每个城市之间的所有可能距离?在

谢谢你的帮助!在


Tags: in距离city内容列表mdil例子
2条回答

像这样的事情可能会奏效。。。在

resultDict = {}
for startCity in places:
    for endCity in places:
        startLocation = startCity[0] + ' ' + startCity[1]
        endLocation = endCity[0] + ' ' + endCity[1]
        resultLocations = [startLocation, endLocation]
        resultId = ','.join(resultLocations.sort())

        if startLocation == endLocation or resultId in resultDict:
            continue

        dlat = math.radians(float(startCity[2])-float(endCity[2]))
        dlon = math.radians(float(startCity[3])-float(endCity[3]))
        a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(float(endCity[2]))) \
            * math.cos(math.radians(float(startCity[2]))) * math.sin(dlon/2) * math.sin(dlon/2)
        c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
        d = radius * c
        resultDict[resultId] = d * 0.621371

print resultDict

你的代码很难阅读,这就模糊了你要做的事情。对于四个位置,您需要(4 x(4-1))/2=6个距离,但是随着位置计数的增加,数量迅速增加。在这里,我退出并计算出完整的4x4距离矩阵-很明显,我做了一些多余的工作,你可以消除这些工作。在

您的下标使您很难理解发生了什么,因此我在for循环中使用解包赋值来避免它,并将数据的每个元素绑定到一个名称。在

import math
places = [
    ["Millersville", "CA", 35.303850, -118.457860],    #(city 1)
    ["Millersville", "IL", 39.451150, -89.158140 ],    #(city 2)
    ["Millersville", "IN", 39.853100, -86.091650 ],     #(city 3)
    ["Millersville", "MD", 39.059550, -76.648020 ]      #(city 4)
]
# One of the minus signs was weird
radius = 6371 # Mean radius in Km
distances = []
for nameA, stateA, latA, longA in places:
    dists_row = []
    for nameB, stateB, latB, longB in places:
        dlat = math.radians(latA-latB)
        dlon = math.radians(longA-longB)
        a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(latA)) \
            * math.cos(math.radians(latB)) * math.sin(dlon/2) * math.sin(dlon/2)
        c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
        d = radius * c
        distance = d * 0.621371
        dists_row.append(distance)
    distances.append(dists_row)

for row in distances:
    print(row)

这个打印出来了

^{pr2}$

这是很好的对称和令人放心的零在对角线,这意味着我可能没有把你的公式,因为我重写他们。这是一个打印结果而不是存储在矩阵中的版本。在这一次,我只计算一次每个距离。在

radius = 6371 # Mean radius in Km
for stnum, (nameA, stateA, latA, longA) in enumerate(places):
    for nameB, stateB, latB, longB in places[stnum+1:]:
        dlat = math.radians(latA-latB)
        dlon = math.radians(longA-longB)
        a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(latA)) \
            * math.cos(math.radians(latB)) * math.sin(dlon/2) * math.sin(dlon/2)
        c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
        d = radius * c
        distance = d * 0.621371
        print(nameA, stateA, "to", nameB, stateB, "is", distance, "miles")

从中输出

Millersville CA to Millersville IL is 1626.6140655454842 miles
Millersville CA to Millersville IN is 1789.8845362720158 miles
Millersville CA to Millersville MD is 2295.8829666956335 miles
Millersville IL to Millersville IN is 165.46706149552824 miles
Millersville IL to Millersville MD is 669.3166714199295 miles
Millersville IN to Millersville MD is 506.5281329453448 miles

相关问题 更多 >