2017-03-17 305 views
0

我有一個vehicle['estimatedCalls']['estimatedCall']列表包含以下項目:迭代通過Python列表

[ 
    { 
    u"originDisplay": [], 
    u"destinationDisplay": [], 
    u"stopPointRef": { 
     u"value": "STIF:StopPoint:Q:24684:" 
    }, 
    u"expectedDepartureTime": "2017-03-17T19:00:00.000Z", 
    u"stopPointName": [], 
    u"arrivalOperatorRefs": [] 
    }, 
    { 
    u"originDisplay": [], 
    u"destinationDisplay": [], 
    u"stopPointRef": { 
     u"value": "STIF:StopPoint:Q:24683:" 
    }, 
    u"expectedDepartureTime": "2017-03-17T19:00:00.000Z", 
    u"stopPointName": [], 
    u"arrivalOperatorRefs": [] 
    }, 
    { 
    u"originDisplay": [], 
    u"destinationDisplay": [], 
    u"stopPointRef": { 
     u"value": "STIF:StopPoint:Q:24680:" 
    }, 
    u"expectedDepartureTime": "2017-03-17T19:00:00.000Z", 
    u"stopPointName": [], 
    u"arrivalOperatorRefs": [] 
    }, 
    { 
    u"originDisplay": [], 
    u"destinationDisplay": [], 
    u"stopPointRef": { 
     u"value": "STIF:StopPoint:Q:24687:" 
    }, 
    u"expectedDepartureTime": "2017-03-17T19:00:00.000Z", 
    u"stopPointName": [], 
    u"arrivalOperatorRefs": [] 
    }, 
    { 
    u"originDisplay": [], 
    u"destinationDisplay": [], 
    u"stopPointRef": { 
     u"value": "STIF:StopPoint:Q:24686:" 
    }, 
    u"expectedDepartureTime": "2017-03-17T19:00:00.000Z", 
    u"stopPointName": [], 
    u"arrivalOperatorRefs": [] 
    }, 
    { 
    u"originDisplay": [], 
    u"destinationDisplay": [], 
    u"stopPointRef": { 
     u"value": "STIF:StopPoint:Q:24685:" 
    }, 
    u"expectedDepartureTime": "2017-03-17T19:00:00.000Z", 
    u"stopPointName": [], 
    u"arrivalOperatorRefs": [] 
    } 
] 

我想通過每個stopPointRef迭代,expectedDepartureTime夫婦(和不在座的,有時expectedArrivalTimeaimedDepartureTimeaimedArrivalTime存在時),以檢索它們的值(爲stopPointRef,價值不value但第二項(與STIF:StopPoint:Q:開始)

這裏是我當前的代碼:

for call in vehicle['estimatedCalls']['estimatedCall']: 
    stoptime = ent.trip_update.stop_time_update.add() 
    for j in len(vehicle['estimatedCalls']['estimatedCall']['stopPointRef']): 
     stoptime.stop_id = vehicle['estimatedCalls']['estimatedCall']['stopPointRef']['value'] 
     stoptime.arrival_time = call['expectedArrivalTime'] 
     stoptime.departure_time = call['expectedDepartureTime'] 

的 「for」 循環似乎正常工作(print vehicle['estimatedCalls']['estimatedCall']返回正確的列表)

但是當試圖通過每個stopPointRefexpectedDepartureTime組進行迭代,用:

for j in len(vehicle['estimatedCalls']['estimatedCall']['stopPointRef']): 

我出現以下錯誤:TypeError: list indices must be integers, not str

您能否幫我解決這個問題並找到適當的代碼來執行該操作?謝謝你的幫助!

+3

'len'返回一個整數,所以你寫的東西歸結爲類似'在10' j,它並沒有真正任何意義。有時候,一個人使用'範圍內的j(len(...))',但這看起來並不是你想要的。 – fuglede

+0

this「vehicle ['estimatedCalls'] ['estimatedCall']」是一個不是字典的列表..您必須將它稱爲vehicle ['estimatedCalls'] ['estimatedCall'] [0] ['stopPointRef'] – repzero

回答

1

爲了簡單的目的,並從提供的,這裏的信息是一個示例

value=[{u'originDisplay': [], u'destinationDisplay': [], 
    u'stopPointRef': {u'value': u'STIF:StopPoint:Q:24684:'}, 
    u'expectedDepartureTime': u'2017-03-17T19:00:00.000Z', 
    u'stopPointName': [], u'arrivalOperatorRefs': []}, 
{u'originDisplay': [], u'destinationDisplay': [], 
    u'stopPointRef': {u'value': u'STIF:StopPoint:Q:24683:'}, 
    u'expectedDepartureTime': u'2017-03-17T19:00:00.000Z', 
    u'stopPointName': [], u'arrivalOperatorRefs': []}, 
{u'originDisplay': [], u'destinationDisplay': [], 
    u'stopPointRef': {u'value': u'STIF:StopPoint:Q:24680:'}, 
    u'expectedDepartureTime': u'2017-03-17T19:00:00.000Z', 
    u'stopPointName': [], u'arrivalOperatorRefs': []}, 
{u'originDisplay': [], u'destinationDisplay': [], 
    u'stopPointRef': {u'value': u'STIF:StopPoint:Q:24687:'}, 
    u'expectedDepartureTime': u'2017-03-17T19:00:00.000Z', 
    u'stopPointName': [], u'arrivalOperatorRefs': []}, 
{u'originDisplay': [], u'destinationDisplay': [], 
    u'stopPointRef': {u'value': u'STIF:StopPoint:Q:24686:'}, 
    u'expectedDepartureTime': u'2017-03-17T19:00:00.000Z', 
    u'stopPointName': [], u'arrivalOperatorRefs': []}, 
{u'originDisplay': [], u'destinationDisplay': [], 
    u'stopPointRef': {u'value': u'STIF:StopPoint:Q:24685:'}, 
    u'expectedDepartureTime': u'2017-03-17T19:00:00.000Z', 
    u'stopPointName': [], u'arrivalOperatorRefs': []}] 
#I am attempting to build the vehicle dict like the one you have 
vehicle={} 
vehicle["estimatedCalls"]={"estimatedCall":value} 

for call in vehicle['estimatedCalls']['estimatedCall']: 
    print("\n\n") 
    #stoptime = ent.trip_update.stop_time_update.add() 
    expected_arrival_time= call['expectedArrivalTime'] if 'expectedArrivalTime' in call else None 
    expected_departure_time=call["expectedDepartureTime"] 
    aimed_departuretime=call['aimedDepartureTime'] if 'aimedDepartureTime' in call else None 
    aimed_arrivaltime=call['aimedArrivalTime'] if 'aimedArrivalTime' in call else None 
    print("stop id is",call['stopPointRef']['value']) 
    print("expected Arrival time is",expected_arrival_time) 
    print("expected departure time is",expected_departure_time) 
    print("aimed departure time is",aimed_departuretime) 
    print("aimed arrival time time is",aimed_arrivaltime)