python提取列表中的一样元素_从python中的列表和字典的复杂列表中提取元素

I have a list with a number of listed lists and dictionaries representing NYC subway cars:

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

I am trying to identify the entries associated with a specific stop_id. For example, if I was searching for 'D03N' I would like to return the entire entry associated with it:

{'arrival': {'time': 1506874477L},

'departure': {'time': 1506874477L},

'schedule_relationship': 0,

'stop_id': u'D03N'}

Unfortunately, whenever I try and use the suggestions from this answer: Python list of dictionaries search

I end up with a 'TypeError: list indices must be integers, not str' error message. I'm not sure if this is because I am implementing that solution incorrectly or the solution does not apply because of the relative complexity of this list compared to the one in the original question.

Is there a way to pluck specific entries out of this list?

解决方案l =

[ i for i in sum(l,[]) if i['stop_id'] == 'D03N' ]

or more efficient way

from itertools import chain

[ i for i in chain.from_iterable(l) if i['stop_id'] == 'D03N' ]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值