使用python创建层次结构数据。基于一系列结果
我需要在python中转换层次嵌套字典中的数据列表。结构(父-子) 这是我的数据使用python创建层次结构数据。基于一系列结果,python,python-3.x,dictionary,data-structures,hierarchical-data,Python,Python 3.x,Dictionary,Data Structures,Hierarchical Data,我需要在python中转换层次嵌套字典中的数据列表。结构(父-子) 这是我的数据 list_data = [ { "id": 2, "father_id": 0, "desc": "Oficial de Negocios Senior", "name": "PEDRO MARTIN SOTO ROSALES" }, { "id": 4,
list_data = [
{
"id": 2,
"father_id": 0,
"desc": "Oficial de Negocios Senior",
"name": "PEDRO MARTIN SOTO ROSALES"
},
{
"id": 4,
"father_id": 2,
"desc": "Ejecutivo comercial",
"name": "Adriana Paredez"
},
{
"id": 5,
"father_id": 2,
"desc": "Ejecutivo comercial",
"name": "Hugo Miranda"
},
{
"id": 3,
"father_id": 2,
"desc": "Ejecutivo comercial",
"name": "Mario Azcona"
},
{
"id": 6,
"father_id": 3,
"desc": "vendedor",
"name": "Diana Diaz"
}
]
我已经尝试过这个递归函数,我得到了正确的结构,但是它聚合了前三个子函数的另外两个副本,我真的不需要它。根父元素是父元素id为0的元素
def build(loc_key):
children = {row['id']: {'name': row['name'], 'desc': row['desc'],
'child':[]} for row in list_data if row['father_id'] == loc_key}
data = {}
for key, value in children.items():
data[key] = value
for item in list_data:
if item['father_id'] == key:
data[key]['child'].append(build(key))
return data
print(build(0))
这基本上就是我需要得到的
data = {
2: {'desc': 'Oficial de Negocios Senior',
'name': 'PEDRO MARTIN SOTO ROSALES',
'child':
[
{3: {'desc': 'Ejecutivo comercial',
'name': 'Mario Azcona',
'child': [
{6: {'desc': 'vendedor',
'name': 'Diana Diaz',
'child': []}}]},
4: {'desc': 'Ejecutivo comercial',
'name': 'Adriana Paredez',
'child': []},
5: {'desc': 'Ejecutivo comercial',
'name': 'Hugo Miranda',
'child': []}
PD:我需要以动态的方式支持它,因为用户可以在数据库中添加子节点。我认为问题在于,您的
构建
函数将节点列表作为输入,而不是成对地或以某种其他“较小列表”的方式对单个节点进行操作。因此,递归在这里没有真正的意义。OTOH,一旦您构建了树(顺便说一句,您正试图构建一棵树),那么递归将非常有助于解析结果结构。但是,它对构建树没有多大帮助
这里有一种方法可以构建树。它是O(n)
computetime和memory,但在操作中确实存储了列表的多个副本,因此可能存在一些优化
import pprint
list_data = [
{
"id": 2,
"father_id": 0,
"desc": "Oficial de Negocios Senior",
"name": "PEDRO MARTIN SOTO ROSALES"
},
{
"id": 4,
"father_id": 2,
"desc": "Ejecutivo comercial",
"name": "Adriana Paredez"
},
{
"id": 5,
"father_id": 2,
"desc": "Ejecutivo comercial",
"name": "Hugo Miranda"
},
{
"id": 3,
"father_id": 2,
"desc": "Ejecutivo comercial",
"name": "Mario Azcona"
},
{
"id": 6,
"father_id": 3,
"desc": "vendedor",
"name": "Diana Diaz"
}
]
def tree_structure(list_data):
#build the requisite data structure in a "flat" way... you can initialize this "as you go" in the loop below if more optimization is needed.
data = {row["id"]: {"desc": row["desc"], "name": row["name"], "child": {}} for row in list_data}
root_id = None
for row in list_data:
if row["father_id"] != 0:
data[row["father_id"]]["child"][row["id"]] = data[row["id"]] #note that this stores only a reference to the child dictionary, so it is O(1) memory
else:
root_id = row["id"] #we need this later
return {root_id: data[root_id]}
pprint.pprint(tree_structure(list_data))
请注意,
pprint
以与预期输出不同的顺序打印desc
、name
和child
。这是因为字典没有内在的顺序,结果数据结构是相同的。我决定用另一种方法,但我认为这比我的方法好