【问题标题】:Iterate through nested dictionaries from a JSON file遍历 JSON 文件中的嵌套字典
【发布时间】:2021-12-28 10:14:05
【问题描述】:

我有一个 json 文件的以下输出

    {
  "Threshold": 0.6,
  "Services": [
    {
      "Name": "Service1",
      "Query": [
        "query1",
        "query2",
        "query3"
      ],
      "Products": [
        {
          "Name": "product1",
          "Query": [
            "query4",
            "query5"
          ],
          "Threshold": 0.75
        },
        {
          "Name": "product2",
          "Query": [
            "query6",
            "query7",
            "query8"
          ],
          "Threshold": 0.75
        },
        {
          "Name": "product3",
          "Query": [
            "query9",
            "query10"
          ],
          "Threshold": 0.75
        },
        {
          "Name": "product4",
          "Query": [
            "query11",
            "query12"
          ],
          "Threshold": 0.75
        },
        {
          "Name": "product5",
          "Query": [
            "query13",
            "query14"
          ],
          "Threshold": 0.75
        }
      ]
    },
    {
      "Name": "Service2",
      "Query": [
        "query1",
        "query2",
        "query3"
      ],
      "Products": [
        {
          "Name": "product1",
          "Query": [
            "query4",
            "query5"
          ],
          "Threshold": 0.75
        },
        {
          "Name": "product2",
          "Query": [
            "query6",
            "query7",
            "query8"
          ],
          "Threshold": 0.75
        },
        {
          "Name": "product3",
          "Query": [
            "query9",
            "query10"
          ],
          "Threshold": 0.75
        },
        {
          "Name": "product4",
          "Query": [
            "query11",
            "query12"
          ],
          "Threshold": 0.75
        },
        {
          "Name": "product5",
          "Query": [
            "query13",
            "query14"
          ],
          "Threshold": 0.75
        }
      ]
    }
  ]
}

文件结构如下: 有两个服务,每个服务有五个相似的产品。每个服务都有一个查询列表,其中包含一些描述该服务的关键字。

这同样适用于产品。

每个产品都是一个查询列表,其中包含描述单个产品的关键字。

我想循环进入服务并选择 service1。然后,我想针对文本运行算法中的查询,以查找文本中是否存在一个或所有查询。如果存在一个或多个查询,我想进入产品并开始迭代 product1 到 product5。如果没有,它应该跳过并转到服务2

我想对 service2 和相应的产品做同样的事情。

只有运行以下代码才能运行:

for service in configfile["Services"]:
        if service["Name"] == "Service1":

代码必须在没有硬编码“Service1 或“Service2”名称的情况下运行。

本质上,我想获取 service1 和 service2 并访问查询。我将从Sentence transformers 运行代码。

# Query sentences:
queries = ['query1', 'query2', 'query3']



top_k = min(5, len(corpus))
for query in queries:
    query_embedding = embedder.encode(query, convert_to_tensor=True)

   # We use cosine-similarity and torch.topk to find the highest 5 scores
    cos_scores = util.pytorch_cos_sim(query_embedding, corpus_embeddings)[0]
    top_results = torch.topk(cos_scores, k=top_k)

    print("\n\n======================\n\n")
    print("Query:", query)
    print("\nTop 5 most similar sentences in corpus:")

    for score, idx in zip(top_results[0], top_results[1]):
         print(corpus[idx], "(Score: {:.4f})".format(score))

我从这个算法中为每个查询获得了一些分数。然后我对查询进行排序,并获取具有最大值的查询。如果此值高于阈值,我想在 service1 中的 product1 到 product5 的查询中继续使用相同的算法。取最大值,如果该值高于阈值,我会将其作为“正数”添加到字典中。如果没有,我将其添加为“负面”。

如果该值低于阈值,我想跳过对产品的迭代并转到 Service2 并再次为查询运行算法并重复该过程。

【问题讨论】:

    标签: python dictionary iteration


    【解决方案1】:

    不清楚您想对您的产品做什么,但由于 ServicesProducts 是列表,您可以简单地遍历它们:

    for serv in json_data.get("Services", []):
        if serv.get("Query", None):
            for prod in serv.get("Products", []):
                # do your stuff with prod
                print(serv.get("Name", None), prod.get("Name", None))
    

    输出:

    Service1 product1
    Service1 product2
    Service1 product3
    Service1 product4
    Service1 product5
    Service2 product1
    Service2 product2
    Service2 product3
    Service2 product4
    Service2 product5
    

    【讨论】:

    • 我编辑了我的原始答案。我希望它有所帮助。
    猜你喜欢
    • 1970-01-01
    • 2019-11-11
    • 2017-07-25
    • 2013-07-21
    相关资源
    最近更新 更多