简体   繁体   中英

How do I extract strings from a nested dictionary in Python

I have a return from an api function that gives me a dict like this:

{'result_index': 0,
 'results': [{'final': True,
   'alternatives': [{'transcript': "tickets are available ",
     'confidence': 0.49}]},
  {'final': True,
   'alternatives': [{'transcript': 'everything in the mail ',
     'confidence': 0.61}]},
  {'final': True,
   'alternatives': [{'transcript': 'thanks one all ',
     'confidence': 0.73}]}]}

There could be any number of transcripts included, including 0. How do I extract all the transcripts into one string so it would be like this:

 'tickets are available everything in the mail thanks one all '

Assuming you only want the transcript of the first alternative, the following code should work:

response = {
    "result_index": 0,
    "results": [
        {
            "final": True,
            "alternatives": [
                {"transcript": "tickets are available ", "confidence": 0.49}
            ],
        },
        {
            "final": True,
            "alternatives": [
                {"transcript": "everything in the mail ", "confidence": 0.61}
            ],
        },
        {
            "final": True,
            "alternatives": [{"transcript": "thanks one all ", "confidence": 0.73}],
        },
    ],
}

transcripts = ""
for res in response["results"]:
    transcripts += res["alternatives"][0]["transcript"]

This would work on all results/alternatives/transcripts.

d = {'result_index': 0,
 'results': [{'final': True,
   'alternatives': [{'transcript': "tickets are available ",
     'confidence': 0.49}]},
  {'final': True,
   'alternatives': [{'transcript': 'everything in the mail ',
     'confidence': 0.61}]},
  {'final': True,
   'alternatives': [{'transcript': 'thanks one all ',
     'confidence': 0.73}]}]}

l = []

for x in d["results"]:
    for y in x["alternatives"]:
        l.append(y.get("transcript", ""))

print("".join(l))

One possible approach would be

output = "".join(k.get('transcript', "") for res in response['results'] for k in res.get('alternatives', []))

Using get with alternative defaults here as you've said that there might be nothing in there. Assuming that there are always results. If not, just use `response.get('results', {}).

This should do it, including looking out for missing elements:

res = {'result_index': 0,
 'results': [
    {'final': True,
   'alternatives': [
        {'transcript': "tickets are available ",
     'confidence': 0.49
        }
      ]
    },
    {'final': True,
   'alternatives': [
        {'transcript': 'everything in the mail ',
     'confidence': 0.61
        }
      ]
    },
    {'final': True,
   'alternatives': [
        {'transcript': 'thanks one all ',
     'confidence': 0.73
        }
      ]
    }
  ]
}

ts = [
    alt.get('transcript','')
    for r in (res['results'] if res.get('results') else [])
    for alt in (r['alternatives'] if r.get('alternatives') else [])
]
print("".join(ts))

Output:

tickets are available everything in the mail thanks one all

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