我正在写一个贯穿字典的函数

时间:2019-04-24 13:17:33

标签: python

我正在编写一个函数def fromToVia(flightsD,fromCity,toCity):该函数将所有直航通知用户,但是如果不存在直航,则会找到一条多条航线。到达航班必须在从同一位置出发之前到达。航班不必通过同一家航空公司。

这是我正在使用的字典:

flightsD={"Delta":{1102:[["IND",1850],["MDW",1955]],
                   1096:[["PHX",900],["MDW",1255]],
                   1445:[["ATL",1135],["LAX",1810]],
                   1776:[["PHL",1350],["RAP",1610]],
                   1226:[["PHX",950],["MDW",1345]],
                   1885:[["ATL",1305],["LAX",2000]],
                   1009:[["MDW",1850],["IND",1955]],
                   9001:[["MDW",2145],["IND",2255]]},
          "Southwestern":{1111:[["SAT",430],["MDW",825]],
                          2121:[["MDW",430],["SAT",825]],
                          4335:[["PHX",450],["MDW",745]],
                          1102:[["MDW",1100],["PHX",1450]]},
          "American":{7765:[["IND",1850],["CHA",2105]],
                   2133:[["BNA",900],["IND",1115]],
                   3321:[["HOU",1335],["ATL",1615]],
                   2100:[["BNA",900],["IND",1115]],
                   4311:[["HOU",905],["ATL",1255]],
                   5577:[["ATL",1100],["HOU",1350]],
                   1102:[["BNA",1100],["HOU",1450]]}  }

这是我已经尝试过的代码:

def fromToVia(flightsD,fromCity,toCity):
    for aLines in airD:
        print(aLines)
        newList=[]
        for flightNum in airD[aLines]:
            if airD[aLines][flightNum][0][0]==fromCity and airD[aLines][flightNum][1][0]==toCity:
                tempList=[]
                tempList.append(airD[aLines][flightNum][0][1])
                tempList.append(airD[aLines][flightNum][1][1])
                tempList.append(flightNum)
                newList.append(tempList)
        newList.sort()
        print(newList)
fromToVia(flightsD,'IND','CHA')

这些是我需要的结果。

>>> fromToVia(flightsD,"IND","MDW")
FROM: IND TO: MDW
Delta 1102 is a direct from IND to MDW

>>> fromToVia(flightsD,"BNA","MDW")
FROM: BNA TO: MDW
American 2133 BNA  900 IND 1115
   Delta 1102 IND 1850 MDW 1955

0 个答案:

没有答案