Abraham P.
Abraham P.

Reputation: 35

Iterate through a python list

I have a vehicle['estimatedCalls']['estimatedCall'] list that contains the following items:

[
  {
    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": []
  }
]

I want to iterate through each stopPointRef, expectedDepartureTime couple (and not present here, sometimes expectedArrivalTime, aimedDepartureTime and aimedArrivalTime when present), to retrive their values (for stopPointRef, the value is not value but the second item (that starts with STIF:StopPoint:Q:).

Here is my current code:

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

The "for" loop seems to work properly (print vehicle['estimatedCalls']['estimatedCall'] returns the proper list)

But when trying to iterate through each stopPointRef, expectedDepartureTime group, with:

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

I got the following error: TypeError: list indices must be integers, not str

Could you please help me fix this and find appropriate code to perform that? Thanks for your help!

Upvotes: 0

Views: 89

Answers (1)

repzero
repzero

Reputation: 8402

For simplicity purpose and from the info provided, here is a sample

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)

Upvotes: 1

Related Questions