Skip to main content
 首页 » 编程设计

python之提取字典列表中特定键的所有值之使用机器人框架

2025年05月04日18jyk

我不知道如何使用机器人框架在下面的字典列表中提取键“名称”的所有值。请帮帮我。

{'data': [{'attributes': {'name': 'Acura'}, 
           'id': 'de3fe2a2-eaa1-45d8-8d6b-298e0b2d0666', 
           'type': 'brands'}, 
          {'attributes': {'name': 'Alfa Romeo'}, 
           'id': '52b33ad8-71ab-4fdb-b877-a6080e86439c', 
           'type': 'brands'}, 
          {'attributes': {'name': 'Audi'}, 
           'id': '5f627b7a-193a-40a0-9db7-b8b504177435', 
           'type': 'brands'}, 
          {'attributes': {'name': 'BMW'}, 
           'id': 'dad04367-2248-45b3-b523-0be7cc7bfbad', 
           'type': 'brands'}, 
          {'attributes': {'name': 'Buick'}, 
           'id': '64f77989-3007-41f5-b5c4-8cb5f4a25d45', 
           'type': 'brands'}, 
          {'attributes': {'name': 'Cadillac'}, 
           'id': 'a457ba4e-5f74-4b9a-9316-d26962284983', 
           'type': 'brands'}, 
          {'attributes': {'name': 'Chevrolet'}, 
           'id': '118e4876-993d-4ad0-91f4-ae3d25b1e5f5', 
           'type': 'brands'}], 
 'meta': {'total': 48}} 

请您参考如下方法:

让 d 是字典的名称。可以使用以下代码提取名称:

names = [a['attributes']['name'] for a in d['data']] 

结果:

print names 
[u'Acura', u'Alfa Romeo', u'Audi', u'BMW', u'Buick', u'Cadillac', u'Chevrolet']