简体   繁体   English

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

[英]Extract elements from complex list of lists and dictionaries in 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. 我试图识别与特定stop_id相关的条目。 For example, if I was searching for 'D03N' I would like to return the entire entry associated with it: 例如,如果我正在搜索“ D03N”,我想返回与其关联的整个条目:

 {'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. 不幸的是,每当我尝试使用以下答案中的建议时: Python搜索字典列表时,都会出现“ TypeError:列表索引必须为整数,而不是str”的错误消息。 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' ]

or more efficient way 或更有效的方法

from itertools import chain
[ i for i in chain.from_iterable(l) if i['stop_id'] == 'D03N' ]
>>> from itertools import chain
>>> data = [[{'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'}]]

>>> def find(s):
        found = [x for x in chain(*data) if x['stop_id']==s]
        return found[0] if found else None

>>> find(u'D03N')
{'arrival': {'time': 1506874477L}, 'schedule_relationship': 0, 'departure': {'time': 1506874477L}, 'stop_id': u'D03N'}

Here is a recursive solution that works with any level of nested lists. 这是适用于任何级别的嵌套列表的递归解决方案。 This function searches (DFS) the list like it's a graph where your list is the root node, sub lists are parent nodes and dictionaries are leafs nodes. 此函数搜索(DFS)列表,就像它是一个图形,其中列表是根节点,子列表是父节点,字典是叶节点。

def find_by_stopid(at, target, saveto):
    if isinstance(at, dict):
        if at['stop_id'] == target:
            saveto.append(at)
        return

    for x in at:
        find_by_stopid(x, target, saveto)

found = []
target = u'D03N'

# data is the list you have, targets is the string to match
# and found is where matches are saved
find_by_stopid(data, target, found)

print(found)

You can try this: 您可以尝试以下方法:

entry = 'D03N'
final_entries = [[b for b in i if b["stop_id"] == entry] for i in entry_data]
try:
   new_final_entries = [i for i in final_entries if i][0][0]
except:
   print("Entry not found")

Where entry data is the full dictionary posted in the original question. 输入数据是原始问题中完整的词典。

Output: 输出:

{'arrival': {'time': 1506874477L}, 'schedule_relationship': 0, 'departure': {'time': 1506874477L}, 'stop_id': u'D03N'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM