嵌套列表中的嵌套字典 [英] Nested dictionary from nested list

查看:333
本文介绍了嵌套列表中的嵌套字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似的嵌套列表:.

I have nested list like:.

[['A', 'A1'], ['A1', 'B', 'C'], ['B', 'B1', 'B2'], ['B1', 'b1', 'b2', 'b3',
 'B2', 'd1', 'd2', 'd3', 'd4'], ['C', 'C1', 'C2', 'C3'], ['C1', 'a1', 'a2', 'a3','C2', 'n1', 'n2', 'n3', 'n4','C3', 'x1', 'x2', 'x3', 'x4']]

我想创建嵌套字典,例如:

I want to create the nested dictionary like:

{'A': {'A1': {'B': {'B1': {'b1': {}, 'b2': {}, 'b3': {}},
                    'B2': {'d1': {}, 'd2': {}, 'd3': {}, 'd4': {}}},
              'C': {'C1': {'a1': {}, 'a2': {}, 'a3': {}},
                    'C2': {'n1': {}, 'n2': {}, 'n3': {}, 'n4': {}},
                    'C3': {'x1': {}, 'x2': {}, 'x3': {}, 'x4': {}}}}}}                                                                                   

或:

{'A': {'A1': {'B': {'B1': ['b1', 'b2' 'b3', 'b4'],
                    'B2': ['d1', 'd2', 'd3', 'd4']},
              'C': {'C1': ['a1', 'a2', 'a3'],
                    'C2': ['n1', 'n2', 'n3', 'n4'],
                    'C3': ['x1', 'x2', 'x3', 'x4']}}}                        

我尝试过类似的方法.

I have tried something like.

d = {}
for path in in nested_list:                                                
    current_dict = d                                                    
     for part in path:                                                            
          if part not in current_ dict:                                    
                current_dict [part]={}                                                         

但未获得理想的结果

示例文件

[1.txt]( 4.txt 5.txt

[1.txt](https://i.stack.imgur.com/dIYmb.jpg[2.txt(https://i.stack.imgur.com/brDUz.jpg)[3.txt(https://i.stack.imgur.com/HaTwd.jpg) 4.txt 5.txt

推荐答案

编辑说明:在我给出此答案后,您已经更改了问题中的输入.以下解决方案适用于您的旧输入,其中列表列表为:

EDIT NOTE: You have changed the input in your question after I gave this answer. The following solution pertains to your old input, where the list of lists was:

[['A', 'A1'], ['A1', 'B', 'C'], ['B', 'B1', 'B2'], ['B1', 'b1', 'b2', 'b3'],
 ['B2', 'd1', 'd2', 'd3', 'd4'], ['C', 'C1', 'C2', 'C3'], ['C1', 'a1', 'a2', 'a3'],
 ['C2', 'n1', 'n2', 'n3', 'n4'], ['C3', 'x1', 'x2', 'x3', 'x4']]

假设列表列表存储在变量l中,则可以使用以下for循环使用字典映射p来构造希望字典,以跟踪每个键的父节点:

Assuming your list of lists is stored in variable l, you can use the following for loop to construct the desire dictionary with a dictionary mapping p to keep track of the parent node for each key:

d = {}
p = {}
for k, *s in l:
    r = p.get(k, d)[k] = {i: {} for i in s}
    p.update({i: r for i in s})

d将变为:

{'A': {'A1': {'B': {'B1': {'b1': {}, 'b2': {}, 'b3': {}},
                    'B2': {'d1': {}, 'd2': {}, 'd3': {}, 'd4': {}}},
              'C': {'C1': {'a1': {}, 'a2': {}, 'a3': {}},
                    'C2': {'n1': {}, 'n2': {}, 'n3': {}, 'n4': {}},
                    'C3': {'x1': {}, 'x2': {}, 'x3': {}, 'x4': {}}}}}}

要使叶节点成为列表,可以从下至上遍历树,并使用集合m跟踪子节点,以便可以使用集合差异找到要添加到子节点的所有顶部节点.最后是主词典d:

To make leaf nodes a list, you can traverse the tree from the bottom up and use a set m to keep track of the child nodes so that you can use set difference to find all the top nodes to add to the main dictionary d in the end:

p = {}
m = set()
while l:
    k, *s = l.pop()
    p[k] = {i: p[i] for i in s} if all(i in p for i in s) else s
    m.update(s)
d = {i: p[i] for i in p.keys() - m}

d将变为:

{'A': {'A1': {'B': {'B1': ['b1', 'b2', 'b3'],
                    'B2': ['d1', 'd2', 'd3', 'd4']},
              'C': {'C1': ['a1', 'a2', 'a3'],
                    'C2': ['n1', 'n2', 'n3', 'n4'],
                    'C3': ['x1', 'x2', 'x3', 'x4']}}}}

这篇关于嵌套列表中的嵌套字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆