遍历树(多嵌套)

2024-10-01 04:54:01 发布

您现在位置:Python中文网/ 问答频道 /正文

如果嵌套多达10个级别,且每个级别具有不同的长度,则存在父级、子级、孙辈和更多类似于下面给出的json的关系(id在每个列表中是唯一的)。找到uni_code将另一个对象(子对象)插入其列表的最佳方法是什么

{
  "id": "1",
  "status": "active",
  "created": "",
  "children": [{
      "id": "1",
      "status": "active",
      "created": "",
      "children": [{
          "id": "1",
          "status": "active",
          "created": "",
          "children": [{
              "id": "1",
              "status": "active",
              "created": "",
              "children": [

              ],
              "uni_code": "EGCFJ1"
            },
            {
              "id": "1",
              "status": "active",
              "created": "",
              "children": [

              ],
              "uni_code": "D356RY"
            },

          ],
          "uni_code": "EGCFJ1"
        },


      ],
      "uni_code": "Y7TUP8"
    },


    {
      "id": "4",
      "status": "active",
      "created": "",
      "children": [

      ],
      "uni_code": "WA1JNS"
    },


         ],
  "uni_code": "I429TD"
}

Tags: 对象idjson列表关系statuscode级别
1条回答
网友
1楼 · 发布于 2024-10-01 04:54:01

根据您在评论中提出的后续问题,我相信我现在理解了您的问题……并相应地更新了我的答案

通常,您可以通过使用递归调用自身的函数来遍历递归数据结构(如树)

我的意思是:

def insert_into_tree(tree, new_child, target):
    if tree['uni_code'] == target:
        tree['children'].append(new_child)
        return True
    for child in tree['children']:
        if insert_into_tree(child, new_child, target):
            return True
    return False  # `target` not found.


data = {
    "id": "1",
    "status": "active",
    "created": "",
    "children": [
        {
            "id": "1",
            "status": "active",
            "created": "",
            "children": [
                {
                    "id": "1",
                    "status": "active",
                    "created": "",
                    "children": [
                        {
                            "id": "1",
                            "status": "active",
                            "created": "",
                            "children": [],
                            "uni_code": "EGCFJ1"
                        },
                        {
                            "id": "1",
                            "status": "active",
                            "created": "",
                            "children": [],
                            "uni_code": "D356RY"
                        }
                    ],
                    "uni_code": "EGCFJ1"
                }
            ],
            "uni_code": "Y7TUP8"
        },
        {
            "id": "4",
            "status": "active",
            "created": "",
            "children": [],
            "uni_code": "WA1JNS"
        }
    ],
    "uni_code": "I429TD"
}


new_object = {
    "id": "1",
    "status": "active",
    "created": "",
    "children": [],
    "uni_code": "THX1138"
}

target = "EGCFJ1"
if insert_into_tree(data, new_object, target):
    print(f'new child added to {target!r}')
else:
    print(f'{target!r} not found, new child not added')

相关问题 更多 >