如何从字典列表中获取值为此创建一个函数

ays*_*ysh 0 python dictionary

我有字典,里面有字典列表

  • 字典列表有另一个字典列表

  • 我需要提取值并将其附加到列表中

我需要写一个函数。如果孩子有name附加到父列表,以便任何字典传递函数列表将创建一个输出,如下所示

a = [{"id": "1", "Area": [{"id": "2", "name": "Clinical"},
                          {"id": "23", "name": "Delivery"}]},
     {"id": "2", "Area": [{"id": "2", "name": "Clinical"},
                          {"id": "23", "name": "Delivery"}]}]
Run Code Online (Sandbox Code Playgroud)

预期输出:

[{"id": "1", "Area": ["Clinical", "Delivery"]},
 {"id": "2", "Area": ["Clinical", "Delivery"]}]
Run Code Online (Sandbox Code Playgroud)

代码如下

result = []
temp = {}
for i in range(0,len(a)):
    templist = []
    b = a[i]['Area'][i]['name']
    c = a[i]['id']
    temp['id'] = c
    templist.append(b)
    temp['Area'] = templist
    result.append(temp)
    print (result)
Run Code Online (Sandbox Code Playgroud)

我的输出没有提取并放入列表?

Ric*_*cco 5

这是一个可能的解决方案:

result = [{'id': d['id'], 'Area': [nd['name'] for nd in d['Area']]} for d in a]
Run Code Online (Sandbox Code Playgroud)