获取嵌套列表中的项目级别 [英] Get level of items in a nested list

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

问题描述

问题:

我有一些链接的数据,我想在这张图片上构建一个像这样的结构:

I have some linked data and I want to build a structure like this one on this picture :

并获得每个项目的级别,因为将来我将凝视我的树形结构的最低级别来进行一些计算.

and get the level of each item because in the future I will make some calculations by staring at the lowest level of my tree structure.

预期结果:

我需要一个可以为我提供每个等级物品的结构:

I need to get a structure that gives me items per level :

  • 0级:A
  • 级别1:A = B,C,D
  • 级别2:D = E,F,G
  • 第3级:E = H,I,J,K

到目前为止我已经尝试过:

我已经尝试过使用此递归代码来模拟行为,但是我无法使项目的级别达到标准.

I've tried this recursive code to simulate the behavior but I'm unable to get items the level of items.

dict_item = {"A": ["B","C","D"], "D": ["E","F","G"], "E":["H","I","J"]}
def build_bom(product):
    if not dict_item.get(product):
        return product
    else :
        return [build_bom(x) for x in dict_item.get(product)]
print(build_bom("A"))

我的输出是这样的嵌套列表:

My output is a nested list like this :

['B', 'C', [['H', 'I', 'J'], 'F', 'G']]

我的问题:

我不确定这是否是解决我的问题的最佳方法. 以及如何获得所需的输出? 这是所需的输出:

I'm not sure if this is the best approach to handle my problem. And how to get the desired output? here is the desired output :

[ {"parent_E":["H", "I", "J"]}, 
{"parent_D": ["E", "F", "G"]},
{"parent_A"} :["D","C","B"]},
]

词典列表(其中键是父级,值是子级),列表中的第一个元素是我的结构的最低级别,最后一个是最高的元素.

A list of dictionaries ( where keys are parents and values are children), the first element in the list is the lowest level of my structure and the last is the highest element.

PS:这是一个模拟,但是将来,我将不得不使用此代码处理大型数据集. 任何帮助将不胜感激

PS: This is a simulation but in future, I will have to works on large datasets with this code. Any Help will be appreciated

推荐答案

这就是我将解决此问题的方式.首先,我将从您的dict_item对象生成树.

This is how I will approach this problem. First, I'll generate the tree from your dict_item object.

dict_item = {"A": ["B","C","D"], "D": ["E","F","G"], "E":["H","I","J"]}

def build_tree(x):
    if x in dict_item:
        return {x: [build_tree(v) for v in dict_item[x]]}
    else:
        return x

tree = build_tree("A")
print(tree)
>>> {'A': ['B', 'C', {'D': [{'E': ['H', 'I', 'J']}, 'F', 'G']}]}

然后,在树上进行广度优先的搜索.每次我们击中有子元素的元素时,我们都会将其附加到列表中:

Then, do a breadth-first search on the tree. Each time we hit an element that has children, we append it to a list:

results = []
queue = [tree]

while queue:
    x = queue.pop(0)
    if isinstance(x, dict):
        parent, children = list(x.items())[0]
        results.append({'parent_' + parent: dict_item[parent]})
        for child in children:
            queue.append(child)

print(results)
>>> [{'parent_A': ['B', 'C', 'D']}, {'parent_D': ['E', 'F', 'G']}, {'parent_E': ['H', 'I', 'J']}]

那么我们现在要做的就是反转列表:

Then all we need to do now, is to reverse the list:

print list(reversed(results))
>>> [{'parent_E': ['H', 'I', 'J']}, {'parent_D': ['E', 'F', 'G']}, {'parent_A': ['B', 'C', 'D']}]

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

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