从python中的列表和字典的复杂列表中提取元素 [英] Extract elements from complex list of lists and dictionaries in python

查看:557
本文介绍了从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'}]]

我正在尝试识别与特定stop_id相关的条目.例如,如果我正在搜索"D03N",我想返回与其关联的整个条目:

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

不幸的是,每当我尝试使用此答案中的建议时: Python词典搜索列表 我最终遇到"TypeError:列表索引必须是整数,而不是str"错误消息.我不确定这是因为我没有正确实现该解决方案,还是因为该列表与原始问题中的列表相比相对复杂,所以该解决方案不适用.

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 = <your list>
[ i for i in sum(l,[]) if i['stop_id'] == 'D03N' ]

或更有效的方式

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

这篇关于从python中的列表和字典的复杂列表中提取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆