简体   繁体   English

我如何迭代 boto3 中的嵌套字典和列表以获得特定值?

[英]How can I itterate over nested dictionaries and lists in boto3 to obtain particular values?

I'm trying to itterate over these values to retrive the Tags to see if any of the tag values match "AWSNetworkFirewallManaged" .我正在尝试迭代这些值以检索标签以查看是否有任何标签值匹配"AWSNetworkFirewallManaged"

I'm having problems figuring out a solution to achieve this.我在找出实现此目标的解决方案时遇到问题。

response = {
    "VpcEndpoints": [
        {
            "VpcEndpointId": "vpce-123",
            "VpcEndpointType": "GatewayLoadBalancer",
            "VpcId": "vpc-test",
            "ServiceName": "com.amazonaws.com",
            "State": "available",
            "SubnetIds": [
                "subnet-random"
            ],
            "IpAddressType": "ipv4",
            "RequesterManaged": True,
            "NetworkInterfaceIds": [
                "eni-123"
            ],
            "CreationTimestamp": "2022-10-28T01:23:23.924Z",
            "Tags": [
                {
                    "Key": "AWSNetworkFirewallManaged",
                    "Value": "true"
                },
                {
                    "Key": "Firewall",
                    "Value": "arn:aws:network-firewall:us-west-2"
                }
            ],
            "OwnerId": "123"
        },
        {
            "VpcEndpointId": "vpce-123",
            "VpcEndpointType": "GatewayLoadBalancer",
            "VpcId": "vpc-<value>",
            "ServiceName": "com.amazonaws.vpce.us-west-2",
            "State": "available",
            "SubnetIds": [
                "subnet-<number>"
            ],
            "IpAddressType": "ipv4",
            "RequesterManaged": True,
            "NetworkInterfaceIds": [
                "eni-<value>"
            ],
            "CreationTimestamp": "2022-10-28T01:23:42.113Z",
            "Tags": [
                {
                    "Key": "AWSNetworkFirewallManaged",
                    "Value": "True"
                },
                {
                    "Key": "Firewall",
                    "Value": "arn:aws:network-firewall:%l"
                }
            ],
            "OwnerId": "random"
            }
        ]
    }

So far I have到目前为止我有

for endpoint in DESCRIBE_VPC_ENDPOINTS['VpcEndpoints']:
    print(endpoint['VpcEndpointId']['Tags']

However this needs to be indice, but if it is I do not know if it will still itterate over the rest of the VPC endpoint ids.然而,这需要是索引,但如果是,我不知道它是否仍会遍历 VPC 端点 ID 的 rest。

Any suggestions or guidence on this?对此有何建议或指导?

You can use double for loop:您可以使用双 for 循环:

for endpoint in response['VpcEndpoints']:
  for tags in endpoint['Tags']:
    if 'AWSNetworkFirewallManaged' in tags.values():
     print(endpoint['VpcEndpointId'], tags)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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