【问题标题】:Identify location of item in json file识别 json 文件中项目的位置
【发布时间】:2021-01-29 12:22:48
【问题描述】:

假设我有以下 json 文件。使用 data1["tenants"][1]['name'] 我可以选择 uniquename2。有没有办法通过遍历文档来收集“1”数字?

{
    "tenants": [{
            "key": "identifier",
            "name": "uniquename",
            "image": "url",
            "match": [
                "identifier"
            ],
            "tags": [
                "tag1",
                "tag2"
            ]
        },
        {
            "key": "identifier",
            "name": "uniquename2",
            "image": "url",
            "match": [
                "identifier1",
                "identifier2"
            ],
            "tags": ["tag"]
        }
    ]
}

简而言之:data1["tenants"][1]['name']= 唯一名称2 data1["tenants"][0]['name'] = 唯一名称 我怎样才能找出哪个号码有哪个名字。那么如果我有 uniquename2 对应的数字/索引是什么?

【问题讨论】:

    标签: python json dictionary indexing dictionary-comprehension


    【解决方案1】:

    您可以遍历租户以将索引映射到名称

    data = {
        "tenants": [{
                "key": "identifier",
                "name": "uniquename",
                "image": "url",
                "match": [
                    "identifier"
                ],
                "tags": [
                    "tag1",
                    "tag2"
                ]
            },
            {
                "key": "identifier",
                "name": "uniquename2",
                "image": "url",
                "match": [
                    "identifier1",
                    "identifier2"
                ],
                "tags": ["tag"]
            }
        ]
    }
    
    for index, tenant in enumerate(data['tenants']):
        print(index, tenant['name'])
    

    输出

    0 uniquename
    1 uniquename2
    

    【讨论】:

      【解决方案2】:

      假设您已经将 json 转换为字典,这就是您如何获取列表中第一次出现的名称的索引(这取决于名称实际上是唯一的):

      data = {
          "tenants": [{
                  "key": "identifier",
                  "name": "uniquename",
                  "image": "url",
                  "match": [
                      "identifier"
                  ],
                  "tags": [
                      "tag1",
                      "tag2"
                  ]
              },
              {
                  "key": "identifier",
                  "name": "uniquename2",
                  "image": "url",
                  "match": [
                      "identifier1",
                      "identifier2"
                  ],
                  "tags": ["tag"]
              }
          ]
      }
      
      def index_of(tenants, tenant_name):
          try:
              return tenants.index(
                  next(
                      tenant for tenant in tenants
                      if tenant["name"] == tenant_name
                  )
              )
          except StopIteration:
              raise ValueError(
                  f"tenants list does not have tenant by name {tenant_name}."
              )
      
      index_of(data["tenants"], "uniquename")  # 0
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-20
        • 1970-01-01
        • 1970-01-01
        • 2016-10-23
        • 2010-11-15
        • 2023-03-29
        • 1970-01-01
        • 2016-11-21
        相关资源
        最近更新 更多