嵌套列表中的嵌套字典

编程入门 行业动态 更新时间:2024-10-25 06:25:59
本文介绍了嵌套列表中的嵌套字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有类似的嵌套列表:.

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]( i.stack.imgur/dIYmb.jpg [2.txt(https ://i.stack.imgur/brDUz.jpg)[3.txt(i.stack.imgur/HaTwd.jpg) 4.txt 5.txt

[1.txt](i.stack.imgur/dIYmb.jpg[2.txt(i.stack.imgur/brDUz.jpg)[3.txt(i.stack.imgur/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']}}}}

更多推荐

嵌套列表中的嵌套字典

本文发布于:2023-11-12 03:40:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580403.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   字典   列表中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!