保存每次迭代python的结果

2024-10-03 13:21:32 发布

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

我正在尝试使用QPX Express。我要保存机场的出发地和目的地的每个循环结果。当我发送JSon请求(Origin:ORY/Designation:LAX/Solution 2)时,我通常有2个航班(可能有航班连接)。在

multivol = data['trips']['tripOption']
origine_air = []
destination_air = []
for p in multivol : 
    print("")
    multivol1 = p['slice']
    prix = p['saleTotal']
        print prix
    for q in multivol1 : 
        multivol2 = q['segment']
        duree_trip = q['duration']
        duree_trip_h = duree_trip // 60
        print duree_trip_h
        for s in multivol2 :
            multivol3 = s['leg']
            for d in multivol3 : 
                ori = d['origin']
                dest = d['destination']
                heure_ar = d['arrivalTime']
                heure_de = d['departureTime']
                vol_entier = ori + dest
                print vol_entier
                origine_air.append(ori)

我试图把结果存储在一个列表中。在

我的结果:

^{pr2}$

名单的结果不是我所期望的。当你可以看到从奥利到洛杉矶有一个航班在LRH(伦敦)转机,在列表中只有第一个航班(ORY到LHR),而不是第二个部分。在

我怎么能把所有的行程都列在我的单子上?在

谢谢你

罗宾


Tags: inforairdestinationprint航班tripory
1条回答
网友
1楼 · 发布于 2024-10-03 13:21:32

您的主要关注点似乎是解析一个字符串a,将令牌存储到一个列表中。也许这就是你需要的?在

flights = ["ORYLHR","LHRLAX"]
#Given a list of the flights, parse them by breaking the strings up in half and storing each half in a list
originDest = []
for i in range(0,len(flights)):
    #This gets the first three chars
    origin = flights[i][0:3]

    #This gets the last three chars
    dest = flights[i][3:6]

    #Append
    originDest.append(origin)
    originDest.append(origin)

输出

^{pr2}$

相关问题 更多 >