循环列表的排列并将功能应用于它们

时间:2015-05-08 12:35:04

标签: python algorithm

我正在尝试获取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)函数计算运行所有排列?然后创建一个列表来保存所有这些计算?

2 个答案:

答案 0 :(得分:2)

我认为你在循环开始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...

另一种方法

你可以完全摆脱内循环,它留给读者Zen of Python的哪一部分更重要 - 这更平坦(比嵌套更好),但这是特朗普&# 34;可读性计数" ...

zip使用documented behaviour

  

当最短输入可迭代用尽时,迭代器停止

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...

答案 1 :(得分:1)

我认为你的问题在这里:

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)

当您要追加value

时,您会将route追加到i[value]

或者更好的是,直接遍历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只会让人感到困惑......