【问题标题】:Extract List of Dictionary in Python在 Python 中提取字典列表
【发布时间】:2022-07-12 01:03:44
【问题描述】:

我有以下清单:

    {
   "TargetHealthDescriptions":[
      {
         "Target":{
            "Id":"10.101.100.101",
            "Port":8200,
            "AvailabilityZone":"all"
         },
         "HealthCheckPort":"8200",
         "TargetHealth":{
            "State":"healthy"
         }
      },
      {
         "Target":{
            "Id":"10.102.100.102",
            "Port":8200,
            "AvailabilityZone":"all"
         },
         "HealthCheckPort":"8200",
         "TargetHealth":{
            "State":"healthy"
         }
      },
      {
         "Target":{
            "Id":"10.103.100.103",
            "Port":8200,
            "AvailabilityZone":"all"
         },
         "HealthCheckPort":"8200",
         "TargetHealth":{
            "State":"healthy"
         }
      }
   ],
   "ResponseMetadata":{
      "RequestId":"abcdef-01234-4d84-9aaf-0123456789",
      "HTTPStatusCode":200,
      "HTTPHeaders":{
         "x-amzn-requestid":"870c5177-482b-4d84-9aaf-0123456789",
         "content-type":"text/xml",
         "content-length":"1279",
         "date":"Mon, 11 Jul 2022 15:35:38 GMT"
      },
      "RetryAttempts":0
   }
}

如何提取每个目标 ID?如果 dig 命令不匹配,我基本上需要提取更新这些 IP。到目前为止,我唯一能做的就是打印 ['TargetHealthDescriptions'][0] 但每当我尝试遍历列表时,我都会得到一个“”所以不确定这甚至意味着什么。

【问题讨论】:

  • 那不是列表,是字典。
  • 请显示为您提供此输出的代码。 How to Ask
  • 你是对的@Barmar 我道歉????

标签: python boto3


【解决方案1】:

您需要使用它的键从字典中识别和迭代列表。这本字典的键是the_response['TargetHealthDescriptions']

一旦你有了它,你就可以像任何其他列表一样迭代它,将每个结果视为字典,在其中获取你需要的值,在这种情况下是 ['Target']['Id'],所以它看起来像这样:

for target in the_response['TargetHealthDescriptions']:
    print(target['Target']['Id'])

下面的小例子:

the_response =     {
   "TargetHealthDescriptions":[
      {
         "Target":{
            "Id":"10.101.100.101",
            "Port":8200,
            "AvailabilityZone":"all"
         },
         "HealthCheckPort":"8200",
         "TargetHealth":{
            "State":"healthy"
         }
      },
      {
         "Target":{
            "Id":"10.102.100.102",
            "Port":8200,
            "AvailabilityZone":"all"
         },
         "HealthCheckPort":"8200",
         "TargetHealth":{
            "State":"healthy"
         }
      },
      {
         "Target":{
            "Id":"10.103.100.103",
            "Port":8200,
            "AvailabilityZone":"all"
         },
         "HealthCheckPort":"8200",
         "TargetHealth":{
            "State":"healthy"
         }
      }
   ],
   "ResponseMetadata":{
      "RequestId":"abcdef-01234-4d84-9aaf-0123456789",
      "HTTPStatusCode":200,
      "HTTPHeaders":{
         "x-amzn-requestid":"870c5177-482b-4d84-9aaf-0123456789",
         "content-type":"text/xml",
         "content-length":"1279",
         "date":"Mon, 11 Jul 2022 15:35:38 GMT"
      },
      "RetryAttempts":0
   }
}

for target in the_response['TargetHealthDescriptions']:
    print(target['Target']['Id'])

【讨论】:

    【解决方案2】:

    遍历字典的TargetHealthDescriptions 元素中的列表。

    for thd in obj['TargetHealthDescriptions']:
        id = thd['Target']['Id']
        if dig_does_not_match(id):
            thd['Target']['Id'] = something_else
    

    【讨论】:

      【解决方案3】:
      for description in data['TargetHealthDescriptions']:
          print(description]'Target']['Id']
      

      【讨论】:

        【解决方案4】:

        生成器对象只是意味着列表被延迟获取。您可以使用 list 类包装生成器以将其转换为列表:

        descs = list(obj['TargetHealthDescriptions'])
        

        【讨论】:

          猜你喜欢
          • 2020-07-21
          • 2017-02-16
          • 1970-01-01
          • 2011-07-16
          • 2021-03-08
          • 2021-09-26
          • 1970-01-01
          • 1970-01-01
          • 2017-12-15
          相关资源
          最近更新 更多