如何获取嵌套字典的列表中所有键的路径

时间:2018-07-17 04:18:36

标签: python dictionary key

我想获取列表中嵌套字典的所有键的路径。例如,如果我的字典如下所示

{
"persons": [{
    "id": "f4d322fa8f552",
    "address": {
        "building": "710",
        "coord": "[123, 465]",
        "street": "Avenue Road",
        "zipcode": "12345"
    },
    "cuisine": "Chinese",
    "grades": [{
        "date": "2013-03-03T00:00:00.000Z",
        "grade": "B",
        "score": {
          "x": 3,
          "y": 2
        }
    }, {
        "date": "2012-11-23T00:00:00.000Z",
        "grade": "C",
        "score": {
          "x": 1,
          "y": 22
        }
    }],
    "name": "Shash"
}]
}

我想走一条路 path = [['persons'], ['persons','id'],['persons','address'],['persons','address','building']...]到最后一个键。

我试图遍历整个字典以追加path变量。试图从Print complete key path for all the values of a python nested dictionary中获得一些启发,但是我无法获取列表中的路径。

还有其他可行的方法吗?

2 个答案:

答案 0 :(得分:3)

您可以递归地描述数据结构,这是一种使用队列q与递归的方法。但是很难确定这是否是您要查找的内容,因为它显示了列表索引,但是可以很容易地将它们排除在外:

def get_paths(d):
    q = [(d, [])]
    while q:
        n, p = q.pop(0)
        yield p
        if isinstance(n, dict):
            for k, v in n.items():
                q.append((v, p+[k]))
        elif isinstance(n, list):
            for i, v in enumerate(n):
                q.append((v, p+[i]))   # Change to q.append((v, p)) to remove index

In []:
list(get_paths(d))

Out[]:
[[],
 ['persons'],
 ['persons', 0],
 ['persons', 0, 'id'],
 ['persons', 0, 'address'],
 ['persons', 0, 'cuisine'],
 ['persons', 0, 'grades'],
 ['persons', 0, 'name'],
 ['persons', 0, 'address', 'building'],
 ['persons', 0, 'address', 'coord'],
 ['persons', 0, 'address', 'street'],
 ['persons', 0, 'address', 'zipcode'],
 ['persons', 0, 'grades', 0],
 ['persons', 0, 'grades', 1],
 ['persons', 0, 'grades', 0, 'date'],
 ['persons', 0, 'grades', 0, 'grade'],
 ['persons', 0, 'grades', 0, 'score'],
 ['persons', 0, 'grades', 1, 'date'],
 ['persons', 0, 'grades', 1, 'grade'],
 ['persons', 0, 'grades', 1, 'score'],
 ['persons', 0, 'grades', 0, 'score', 'x'],
 ['persons', 0, 'grades', 0, 'score', 'y'],
 ['persons', 0, 'grades', 1, 'score', 'x'],
 ['persons', 0, 'grades', 1, 'score', 'y'],

答案 1 :(得分:2)

您可以对生成器表达式使用递归:

def get_paths(d, current = []):
  for a, b in d.items():
    yield current+[a]
    if isinstance(b, dict):
      yield from get_paths(b, current+[a])
    elif isinstance(b, list):
      for i in b:
        yield from get_paths(i, current+[a])

final_result = list(get_paths(d))
new_result = [a for i, a in enumerate(final_result) if a not in final_result[:i]]

输出:

[['persons'], ['persons', 'id'], ['persons', 'address'], ['persons', 'address', 'building'], ['persons', 'address', 'coord'], ['persons', 'address', 'street'], ['persons', 'address', 'zipcode'], ['persons', 'cuisine'], ['persons', 'grades'], ['persons', 'grades', 'date'], ['persons', 'grades', 'grade'], ['persons', 'grades', 'score'], ['persons', 'grades', 'score', 'x'], ['persons', 'grades', 'score', 'y'], ['persons', 'name']]