简体   繁体   中英

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. 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..

    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.

You may use next() to find the first item from the protocol_parameters list that matches that criteria. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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