通过字典分类

时间:2019-04-17 19:35:49

标签: python

我需要编写函数来打印所有航空公司的fromCity和toCity中指定的所有航班的帮助。这是我使用def reportAllFlightsFromTo(flightsD,fromCity,toCity)的函数:

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

def reportAllFlightsFromTo (airD,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)

这是我正在使用的字典:

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]]}  }

这是我用当前代码收到的结果:

Delta
[]
Southwestern
[]
American
[[1850, 2105, 7765]]

这是我需要的结果:

reportAllFlightsFromTo(flightsD,"MDW","IND")
       MDW  IND
  NUM  DEP  ARR  AIRLINE   
 1009 1850 1955  Delta     
 9001 2145 2255  Delta

2 个答案:

答案 0 :(得分:1)

def reportAllFlightsFromTo(flightsD,fromCity,toCity):
    for airline_name, flights in flightsD.items():
        for flight_number, flight in flights.items():
            if flight[0][0] == fromCity and flight[1][0] == toCity:
                yield flight_number, flight[0][1], flight[1][1], airline_name

result = list(reportAllFlightsFromTo(flightsD,"MDW","IND"))

print('       MDW  IND')
print('  NUM  DEP  ARR  AIRLINE')
for i in result:
    print(' {} {} {}  {}'.format(*i))

输出:

       MDW  IND
  NUM  DEP  ARR  AIRLINE
 1009 1850 1955  Delta
 9001 2145 2255  Delta

答案 1 :(得分:1)

def reportFlightsFromTo(flights, src, dest):
    matching_flights = {}
    for airline, flight_dict in flights.items():
        for flight_no, flight_info in flight_dict.items():
            if flight_info[0][0] == src and flight_info[1][0] == dest:
                tmp = {flight_no:{'depart':flight_info[0][1], 'arrive':flight_info[1][1]}}
                if matching_flights.get(airline):
                    matching_flights[airline].append(tmp)
                else:
                    matching_flights[airline] = [tmp]
    return matching_flights

此答案与Filip的答案之间的主要区别在于Filip的函数是生成器(请注意使用关键字yield)。我的会返回一个包含信息的数据结构,然后可以根据需要对其进行格式化。

更重要的是相似之处。即,使用.items()迭代字典中的键和值对。这样可以使值在循环中可用,从而避免了长时间访问代码,例如airD[aLines][flightNum][0][0]