Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
遍历python列表_Python_List - Fatal编程技术网

遍历python列表

遍历python列表,python,list,Python,List,我有一个车辆['estimatedCalls']['estimatedCalls']列表,其中包含以下项目: [ { u"originDisplay": [], u"destinationDisplay": [], u"stopPointRef": { u"value": "STIF:StopPoint:Q:24684:" }, u"expectedDepartureTime": "2017-03-17T19:00:00.000Z",

我有一个车辆['estimatedCalls']['estimatedCalls']列表,其中包含以下项目:

[
  {
    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、ExpectedDepartRetime和not present(此处不存在),有时是expectedArrivalTime、AimedDepartRetime和aimedArrivalTime(当存在时),以检索stopPointRef的值,该值不是值,而是以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循环似乎工作正常打印车辆['estimatedCalls']['estimatedCall']返回正确的列表

但在尝试迭代每个stopPointRef、expectedDepartureTime组时,使用:

for j in len(vehicle['estimatedCalls']['estimatedCall']['stopPointRef']):
我得到了以下错误:TypeError:列表索引必须是整数,而不是str


你能帮我解决这个问题并找到合适的代码来执行吗?谢谢你的帮助

为了简单起见,根据提供的信息,这里是一个示例

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)

len返回一个整数,所以你写的东西可以归结为类似于10中的j,这没有任何意义。有时,人们在rangelen中使用j…,但这似乎不是你想要的。这辆车['estimatedCalls']['estimatedCalls']是一个列表,而不是字典。。您必须像车辆一样调用它['estimatedCalls']['estimatedCalls'][0]['stopPointRef']