簡體   English   中英

在 Python 中提取字典列表

[英]Extract List of Dictionary in Python

我有以下列表:

    {
   "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] 但是每當我嘗試遍歷列表時,我都會得到一個“<generator object at 0x10522c890>”所以不確定這甚至意味着什么。

您需要使用它的鍵從字典中識別和迭代列表。 該字典中的鍵是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'])
for description in data['TargetHealthDescriptions']:
    print(description['Target']['Id'])

循環遍歷字典的TargetHealthDescriptions元素中的列表。

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

生成器對象只是意味着正在延遲獲取列表。 您可以使用list類包裝生成器以將其轉換為列表:

descs = list(obj['TargetHealthDescriptions'])

您可以將數據集定義為字典,並使用 for 循環遍歷每個嵌套字典。 可能不是最有效的方法,但它確實完成了工作。

數據集:

dict = {
   "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 x in dict:
    if x == "TargetHealthDescriptions":
       for attr in dict[x]:
          attr_dict = attr
          for y in attr_dict:
             if y == "Target":
                target_dict = attr_dict[y]
                Id = target_dict.get('Id')
                print(Id)

回報:

10.101.100.101
10.102.100.102
10.103.100.103

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM