简体   繁体   English

Python,遍历字典对象中的多个列表以查找特定值

[英]Python, iterate through multiple lists in a dictionary object to find a specific value

Pulling data over a RESTapi, I'm left with a dictionary object containing multiple lists. 通过RESTapi提取数据,剩下一个包含多个列表的字典对象。 I'm looking for a very specific data point within one of the lists, however the actual amount of lists vary with each item in the dictionary. 我正在其中一个列表中寻找非常具体的数据点,但是列表的实际数量随字典中的每个项目而变化。

I've tried to manually pull this field using indexing, etc..., but because the list is not always in the same place, I'm banging my head on the wall. 我尝试使用索引等方式手动拉出该字段,但是由于列表并不总是位于同一位置,所以我将头撞在墙上。 The API results look something like this.. API结果看起来像这样。

    b = [
    {'internal': False, 'protocol_parameters': [{'name': 'identifier', 'id': 1, 'value': 'x.x.x.x'}]},
      {'internal': False, 'protocol_parameters': [{'name': 'identifier', 'id': 0, 'value': 'y.y.y.y'}, {'name': 'incomingPayloadEncoding', 'id': 1, 'value': 'UTF-8'}]},
       {'internal': False, 'protocol_parameters': [{'name': 'incomingPayloadEncoding', 'id': 1, 'value': 'UTF-8'}, {'name': 'identifier', 'id': 0, 'value': 'z.z.z.z'}]}]

for a in b:
     c = (a['protocol_parameters'])[0].get('value')
     print(c)

This of course will not parse correctly because the list is not in a consistent place so I'm curious if I can parse all lists in the dictionary looking for a specific string. 当然,由于列表位置不一致,因此无法正确解析,所以我很好奇我是否可以解析字典中的所有列表以查找特定字符串。 My end goal would be as shown below regardless of the list position. 无论列表位置如何,我的最终目标都将如下所示。

x.x.x.x
y.y.y.y
z.z.z.z

In this example, find all lists that contain "identifier". 在本示例中,找到所有包含“标识符”的列表。 Apologies if this is a noob mistake :) and thank you for your time. 抱歉,如果这是一个菜鸟错误:),谢谢您的时间。

From what I understand you need to pick the value field from the item of protocol_parameters that contains name=identifier. 据我了解,您需要从protocol_parameters项中选择包含name = identifier的value字段。

You may use next() to find the first item from the protocol_parameters list that matches that criteria. 您可以使用next()protocol_parameters列表中找到与该条件匹配的第一项。 See below: 见下文:

records = [{'internal': False, 'protocol_parameters': [{'name': 'identifier', 'id': 1, 'value': 'x.x.x.x'}]},
{'internal': False, 'protocol_parameters': [{'name': 'identifier', 'id': 0, 'value': 'y.y.y.y'}, {'name': 'incomingPayloadEncoding', 'id': 1, 'value': 'UTF-8'}]},
{'internal': False, 'protocol_parameters': [{'name': 'incomingPayloadEncoding', 'id': 1, 'value': 'UTF-8'}, {'name': 'identifier', 'id': 0, 'value': 'z.z.z.z'}]}
]

for record in records:
     identifier_param = next((prot_param for prot_param in record['protocol_parameters'] if prot_param['name']=='identifier'), None)
     if identifier_param:
         print(identifier_param['value'])

prints 版画

x.x.x.x
y.y.y.y
z.z.z.z

This should work: 这应该工作:

b = [{'internal': False, 'protocol_parameters': [{'name': 'identifier', 'id': 1, 'value': 'x.x.x.x'}]},
{'internal': False, 'protocol_parameters': [{'name': 'identifier', 'id': 0, 'value': 'y.y.y.y'}, {'name': 'incomingPayloadEncoding', 'id': 1, 'value': 'UTF-8'}]},
{'internal': False, 'protocol_parameters': [{'name': 'incomingPayloadEncoding', 'id': 1, 'value': 'UTF-8'}, {'name': 'identifier', 'id': 0, 'value': 'z.z.z.z'}]}]

for a in b:
     c = next(param.get('value') for param in a['protocol_parameters'] if param.get('name')=="identifier")
     print(c)

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

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