从多级字典中提取所有路径

2024-05-15 10:00:50 发布

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

我有一本这样的字典:

dirDict = {"DIR1" : {
                    "DIR11" : { 
                                "DIR111" : "Maki111",
                                "DIR112" : "Maki112"
                                }, 
                    "DIR12" : "Maki12", 
                    "DIR13" : {
                                "DIR131" : "Maki131"
                                }
                }
    }

把它想象成一个文件夹结构。我想得到类似于os.walk的文件夹结构。像这样:

["DIR1/DIR11/DIR111/Maki111",
"DIR1/DIR11/DIR112/Maki112",
"DIR1/DIR12/Maki12",
"DIR1/DIR13/DIR131/Maki131"]

所以它基本上是字典值的所有路径。我用递归函数尝试过很多种方法,但都迷路了。你知道吗

这是我的最新试验:

def walk(input_dict, path_string = "",  result = ""):
    for key, value in input_dict.items():
        if isinstance(value, dict):
            path_string += "/" + key
            print "==== DICT ====", "\nkey: ", key, "\nvalue: ", value, "\n\t\tpath_string: ", path_string
            result = walk(value, path_string)
            print "\t\t\t\tresulting: ", result
        elif isinstance(value, str):
            print "==== NOT DICT ===="
            path_string += "/" + value
            print "\t\tpath_string: ", path_string, "\nvalue: ", value
            return path_string
        else:
            path_string = "/" + key
        result += "\n" + result
    return result

Tags: pathkeystring字典valueresultdictwalk
3条回答

使用Python 3:

dirDict = {"DIR1" : {
                    "DIR11" : {
                                "DIR111" : "Maki111",
                                "DIR112" : "Maki112"
                                },
                    "DIR12" : "Maki12",
                    "DIR13" : {
                                "DIR131" : "Maki131"
                                }
                }
    }

def recurse(d, prefix=None, sep='/'):
    if prefix is None:
        prefix = []
    for key, value in d.items():
        if isinstance(value, dict):
            yield from recurse(value, prefix + [key])
        else:
            yield sep.join(prefix + [key, value])

print(list(recurse(dirDict)))

输出:

['DIR1/DIR13/DIR131/Maki131', 'DIR1/DIR11/DIR111/Maki111', 'DIR1/DIR11/DIR112/Maki112', 'DIR1/DIR12/Maki12']

我在https://gist.github.com/nvie/f304caf3b4f1ca4c3884#gistcomment-1597937发布的walk函数可以用作解决问题的助手:

def walk(obj, parent_first=True):

    # Top down?
    if parent_first:
        yield (), obj

    # For nested objects, the key is the path component.
    if isinstance(obj, dict):
        children = obj.items()

    # For nested lists, the position is the path component.
    elif isinstance(obj, (list, tuple)):
        children = enumerate(obj)

    # Scalar values have no children.
    else:
        children = []

    # Recurse into children
    for key, value in children:
        for child_path, child in walk(value, parent_first):
            yield (key,) + child_path, child

    # Bottom up?
    if not parent_first:
        yield (), obj

您的问题可以通过以下方式解决:

for path, value in walk(obj):
    if isinstance(value, str):  # leaf node
        path_with_value = path + (value,)
        print("/".join(path_with_value))
def walk(d, path):
    paths = []
    if len(d) == 0:
        return path
    for k, v in d.iteritems():
        child_path = path + k + '/'
        if isinstance(v, basestring):
            paths.append(child_path + v)
        else:
            paths.extend(walk(v, child_path))
    return paths

相关问题 更多 >