我正在尝试获取5个机场输入的排列列表,以通过距离计算并对每个排列进行计算。
from airports import *
from math import *
import itertools
dicts=TravelLookUp()
dicts.dictAirport('airport.csv')
dicts.dictCurrency('countrycurrency.csv')
dicts.dictCurrencyRates('currencyrates.csv')
#print(airportdict.airportDict)
#airportdict.airportDict[] IS DICTIONARY
#print(test)
#print(AirportDict)
test= dicts.airportDict['DUB'].getLat()
print(test)
airportList=[]
airportHome=[]
airport1=input('Choose the first airport: ').upper()
airport2=input('Choose the second airport: ').upper()
airport3=input('Choose the third airport: ').upper()
airport4=input('Choose the fourth airport: ').upper()
airport5=input('Choose the fifth airport: ').upper()
airportHome=[airport1]
airportList=[airport2,airport3,airport4,airport5]
AirportCombinations = itertools.permutations(airportList,r=4)
def distance(airport1,airport2):
distacnces=[]
lat1=dicts.airportDict[airport1].getLat()
long1=dicts.airportDict[airport1].getLong()
lat2=dicts.airportDict[airport2].getLat()
long2=dicts.airportDict[airport2].getLong()
lat1R= (lat1*pi/180)
long1R=(long1*pi/180)
lat2R=(lat2*pi/180)
long2R=(long2*pi/180)
distance= acos(sin(lat1R)*sin(lat2R)*cos(long1R-long2R))*6373
distances.append(distance)
return distances
for i in AirportCombinations:
i=list(i)
route=[airportHome]
for value in range(len(i)):
route.append(value)
totaldistance=0
for i in range(len(route)-1):
legcost=distance(route[i],route[i+1])
totaldistance+=distance
print(totaldistance)
distance()
路由列表保留排列,我希望它在每个排列中的每个机场对中循环浏览distance(airport1,airport2)
功能。
每当我运行此操作时,它只会计算24次相同两个机场之间的距离,而我希望它要做的就是计算每个组合的距离。
如何通过distance(airport1,airport2)
函数计算运行所有排列?然后创建一个列表以保存所有这些计算?
我认为您在循环中有些复杂的事情开始for i in AirportCombinations
。
我已经简化了该循环(并为清晰重命名您的变量),但保留了您的方法,即转换为每条路线的列表。
您也有一个错别字:我认为totaldistance += distance
应该是totaldistance += legcost
。
for sub_route in AirportCombinations:
route = [airportHome] + list(sub_route)
totaldistance = 0
for i in range(len(route)-1):
legcost = distance(route[i], route[i+1])
totaldistance += legcost
#I've un-indented this so it prints for each permutation
print('route ' + str(route) + ' has distance ' + str(totaldistance))
# Add the distance to a list or dictionary here if you want them available later...
另一种方法
您可以完全摆脱内部循环,剩下的是读者,Python的禅宗的哪一部分更重要 - 这是扁平的(比嵌套更好),但是Trump"可读性计数"是否...
邮政编码利用已记录的行为:
当最短输入峰值耗尽时,迭代器停止
for sub_route in AirportCombinations:
route = [airportHome] + list(sub_route)
totaldistance = sum(distance(a[0], a[1]) for a in zip(route, route[1:]))
print('route ' + str(route) + ' has distance ' + str(totaldistance))
# Add the distance to a list or dictionary here if you want them available later...
我认为您的问题在这里:
for i in AirportCombinations:
i=list(i)
route=[airportHome]
for value in range(len(i)): <<<
route.append(value) <<<
totaldistance=0
for i in range(len(route)-1):
legcost=distance(route[i],route[i+1])
totaldistance+=distance
print(totaldistance)
当您应该附加i[value]
value
附加到route
或更好的是,直接通过i
迭代:
for i in AirportCombinations:
i=list(i)
route=[airportHome]
for value in i:
route.append(value)
totaldistance=0
for i in range(len(route)-1):
legcost=distance(route[i],route[i+1])
totaldistance+=distance
print(totaldistance)
P.S。 - 在两个嵌套环的两个级别中使用i
只是令人困惑...