我有一份清单,上面列出了一些代表纽约地铁车辆的清单和字典:
[[{'arrival': {'time': 1506873749L},
'departure': {'time': 1506873749L},
'schedule_relationship': 0,
'stop_id': u'B20S'},
{'arrival': {'time': 1506873854L},
'departure': {'time': 1506873854L},
'schedule_relationship': 0,
'stop_id': u'B21S'},
{'arrival': {'time': 1506873989L},
'departure': {'time': 1506873989L},
'schedule_relationship': 0,
'stop_id': u'B22S'},
{'arrival': {'time': 1506874184L},
'departure': {'time': 1506874184L},
'schedule_relationship': 0,
'stop_id': u'B23S'},
{'arrival': {'time': 1506874469L},
'departure': {'time': 1506874469L},
'schedule_relationship': 0,
'stop_id': u'D43S'}],
[{'arrival': {'time': 1506873814L},
'departure': {'time': 1506873814L},
'schedule_relationship': 0,
'stop_id': u'D10N'},
{'arrival': {'time': 1506873877L},
'departure': {'time': 1506873877L},
'schedule_relationship': 0,
'stop_id': u'D09N'},
{'arrival': {'time': 1506873997L},
'departure': {'time': 1506873997L},
'schedule_relationship': 0,
'stop_id': u'D08N'},
{'arrival': {'time': 1506874087L},
'departure': {'time': 1506874087L},
'schedule_relationship': 0,
'stop_id': u'D07N'},
{'arrival': {'time': 1506874177L},
'departure': {'time': 1506874177L},
'schedule_relationship': 0,
'stop_id': u'D06N'},
{'arrival': {'time': 1506874267L},
'departure': {'time': 1506874267L},
'schedule_relationship': 0,
'stop_id': u'D05N'},
{'arrival': {'time': 1506874357L},
'departure': {'time': 1506874357L},
'schedule_relationship': 0,
'stop_id': u'D04N'},
{'arrival': {'time': 1506874477L},
'departure': {'time': 1506874477L},
'schedule_relationship': 0,
'stop_id': u'D03N'},
{'arrival': {'time': 1506874627L},
'departure': {'time': 1506874627L},
'schedule_relationship': 0,
'stop_id': u'D01N'}]]
我正在尝试识别与特定stop_id关联的条目。例如,如果我搜索的是'D03N',我希望返回与之关联的整个条目:
^{pr2}$不幸的是,每当我尝试使用这个答案中的建议:Python list of dictionaries search 最后我得到了一条“TypeError:list index must be integers,not str”错误消息。我不确定这是因为我错误地实现了这个解决方案,还是因为这个方案与原始问题中的列表相比相对复杂,所以这个方案不适用。在
有没有办法从这个列表中抽出具体的条目?在
这里是一个递归的解决方案,可以处理任何级别的嵌套列表。 此函数搜索(DFS)列表,就像它是一个图形,其中列表是根节点,子列表是父节点,字典是叶子节点。在
或者更有效的方法
^{pr2}$相关问题 更多 >
编程相关推荐