如何从dict输出中仅提取特定键值?

时间:2018-05-20 16:38:55

标签: python json dictionary

如何提取所有' ImageId'来自低于预期结果的dict结构中的值?

我尝试提取所有值并将它们转换为json并尝试提取,但这似乎并不适合我,或者可能不是执行此任务的好方法。

此代码来自here的Boto3文档。

预期产出:

{
    'SpotFleetRequestConfigs': [
        {
            'SpotFleetRequestConfig': {
                'IamFleetRole': 'arn:aws:iam::123456789012:role/my-spot-fleet-role',
                'LaunchSpecifications': [
                    {
                        'EbsOptimized': False,
                        'ImageId': 'ami-1a2b3c4d',
                        'InstanceType': 'cc2.8xlarge',
                        'NetworkInterfaces': [
                            {
                                'AssociatePublicIpAddress': True,
                                'DeleteOnTermination': False,
                                'DeviceIndex': 0,
                                'SecondaryPrivateIpAddressCount': 0,
                                'SubnetId': 'subnet-a61dafcf',
                            },
                        ],
                    },
                    {
                        'EbsOptimized': False,
                        'ImageId': 'ami-1a2b3c4d',
                        'InstanceType': 'r3.8xlarge',
                        'NetworkInterfaces': [
                            {
                                'AssociatePublicIpAddress': True,
                                'DeleteOnTermination': False,
                                'DeviceIndex': 0,
                                'SecondaryPrivateIpAddressCount': 0,
                                'SubnetId': 'subnet-a61dafcf',
                            },
                        ],
                    },
                ],
                'SpotPrice': '0.05',
                'TargetCapacity': 20,
            },
            'SpotFleetRequestId': 'sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE',
            'SpotFleetRequestState': 'active',
        },
    ],
    'ResponseMetadata': {
        '...': '...',
    },
}

1 个答案:

答案 0 :(得分:1)

您可以使用列表理解:

res = [i['ImageId'] for i in \
       d['SpotFleetRequestConfigs'][0]['SpotFleetRequestConfig']['LaunchSpecifications']]

print(res)

['ami-1a2b3c4d', 'ami-1a2b3c4d']

只使用了2个操作:

  1. 通过d[key]
  2. 访问字典的键
  3. 通过lst[i]
  4. 访问列表的元素 i

    最困难的部分是确定列表和词典的位置。我建议您一步一步地查看输入的每个部分:通过{}标识字典,同时列出[]

    <强>设置

    d = {
        'SpotFleetRequestConfigs': [
            {
                'SpotFleetRequestConfig': {
                    'IamFleetRole': 'arn:aws:iam::123456789012:role/my-spot-fleet-role',
                    'LaunchSpecifications': [
                        {
                            'EbsOptimized': False,
                            'ImageId': 'ami-1a2b3c4d',
                            'InstanceType': 'cc2.8xlarge',
                            'NetworkInterfaces': [
                                {
                                    'AssociatePublicIpAddress': True,
                                    'DeleteOnTermination': False,
                                    'DeviceIndex': 0,
                                    'SecondaryPrivateIpAddressCount': 0,
                                    'SubnetId': 'subnet-a61dafcf',
                                },
                            ],
                        },
                        {
                            'EbsOptimized': False,
                            'ImageId': 'ami-1a2b3c4d',
                            'InstanceType': 'r3.8xlarge',
                            'NetworkInterfaces': [
                                {
                                    'AssociatePublicIpAddress': True,
                                    'DeleteOnTermination': False,
                                    'DeviceIndex': 0,
                                    'SecondaryPrivateIpAddressCount': 0,
                                    'SubnetId': 'subnet-a61dafcf',
                                },
                            ],
                        },
                    ],
                    'SpotPrice': '0.05',
                    'TargetCapacity': 20,
                },
                'SpotFleetRequestId': 'sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE',
                'SpotFleetRequestState': 'active',
            },
        ],
        'ResponseMetadata': {
            '...': '...',
        },
    }