在遍历字典时使用递归函数以填充字典的空列表

时间:2019-01-29 05:45:44

标签: python json

我有一个字典列表,该列表来自JSON文件。这是一个JSON文件(经过有意简化):

[
   {
      "one":{
         "private":{
            "resource":"qwerty"
         },
         "children":[
            "test"
         ],
         "public":{
            "-name":"gf"
         },
         "parents":[
            "twenty"
         ],
         "id":"one",
         "properties":{
            "COLOR":{
               "-type":"string"
            },
            "H":{
               "-type":"double"
            },
            "TO_NOTIFY":{
               "-type":"string"
            },
            "environment":{
               "-type":"string"
            },
            "EMAIL_TO":{
               "-type":"string"
            },
            "W":{
               "-type":"double"
            },
            "Y":{
               "-type":"double"
            },
            "X":{
               "-type":"double"
            }
         }
      }
   },
   {
      "two":{
         "private":{
            "resource":"qwerty"
         },
         "children":[
            "test"
         ],
         "public":{
            "-name":"gf"
         },
         "parents":[
            "one"
         ],
         "id":"two",
         "properties":{
            "COLOR":{
               "-type":"string"
            },
            "H":{
               "-type":"double"
            },
            "TO_NOTIFY":{
               "-type":"string"
            },
            "environment":{
               "-type":"string"
            },
            "EMAIL_TO":{
               "-type":"string"
            },
            "W":{
               "-type":"double"
            },
            "Y":{
               "-type":"double"
            },
            "X":{
               "-type":"double"
            }
         }
      }
   },
   {
      "three":{
         "private":{
            "resource":"qwerty"
         },
         "children":[
            "test"
         ],
         "public":{
            "-name":"gf"
         },
         "parents":[
            "two"
         ],
         "id":"three",
         "properties":{
            "COLOR":{
               "-type":"string"
            },
            "H":{
               "-type":"double"
            },
            "TO_NOTIFY":{
               "-type":"string"
            },
            "environment":{
               "-type":"string"
            },
            "EMAIL_TO":{
               "-type":"string"
            },
            "W":{
               "-type":"double"
            },
            "Y":{
               "-type":"double"
            },
            "X":{
               "-type":"double"
            }
         }
      }
   },
   {
      "four":{
         "private":{
            "resource":"qwerty"
         },
         "children":[
            "test"
         ],
         "public":{
            "-name":"gf"
         },
         "parents":[
            "one"
         ],
         "id":"four",
         "properties":{
            "COLOR":{
               "-type":"string"
            },
            "H":{
               "-type":"double"
            },
            "TO_NOTIFY":{
               "-type":"string"
            },
            "environment":{
               "-type":"string"
            },
            "EMAIL_TO":{
               "-type":"string"
            },
            "W":{
               "-type":"double"
            },
            "Y":{
               "-type":"double"
            },
            "X":{
               "-type":"double"
            }
         }
      }
   }
]

我的目标: :更新当前的JSON(在文件中看到的内容),使其仅包含requested个条目。 Requested在这里意味着用户给了我一些所需的条目,比方说three。我想将three附加到新列表中。另外,我需要找到parents的父three,并将其附加到该列表中。我还需要找到后续条目的父级,依此类推。

我该怎么做?我为此使用递归吗?这是我到目前为止的内容:

import json

with open('/home/intern/nbf/1.json') as f:
    data = json.load(f)

# Finds a parent of a node (and of subsequent nodes if any)
def find_parent(node, data = data):

    l = []

    for i in range(len(data)):
        for k in data[i]:
            if k == node:
                l.append(data[i])

find_parent('three')

我仍然缺少要检查parents并将其添加到列表l中的部分。

1 个答案:

答案 0 :(得分:1)

完成“三”的处理后,您将再次调用该函数(使用相同的列表),但同时使用“三”的两个父项。但是,如果您有周期,则必须小心,因为那样永远不会终止。

def find_parent(node, data, l=None):
    if l is None:
        l = []

    for i in range(len(data)):
        for k in data[i]:
            print(k)

            if k == node:
                l.append(data[i])
                for parent in data[i][k]["parents"]:
                    find_parent(parent, data, l)
    return l