我在尝试转换以下列表时遇到了麻烦:
lst = [
{"id": 0, "job": "CEO", "ManagerID": 0, "name": "John Smith"},
{"id": 1, "job": "Medical Manager", "ManagerID": 0, "name": "Medic 1"},
{"id": 2, "job": "Medical Assist", "ManagerID": 1, "name": "Medic 2"},
{"id": 3, "job": "ICT Manager", "ManagerID": 0, "name": "ICT 1"},
{"id": 4, "job": "ICT Assist", "ManagerID": 3, "name": "ICT 2"},
{"id": 5, "job": "ICT Junior", "ManagerID": 4, "name": "ICT 3"}
]
转换为
output = [
{"id": 0, "job": "CEO", "ManagerID": 0, "name": "John Smith", "children" : [
{ "id":1, "job": "Medical Manager", "name": "Medic 1", "children" : [
{"id": 2, "job": "Medical Assist", "name": "Medic 2"}
]
},
{"id": 3, "job": "ICT Manager", "name": "ICT 1", "children":[
{"id": 4, "job": "ICT Assist", "name": "ICT 2", "children" : [
{"id": 5, "job": "ICT Junior", "name": "ICT 3"}
]}
]}
],
}]
所有其他分支都存在一个根节点(ManagerID = 0)。
我尝试改编another question中的代码,但无法生成此所需格式
我一直在使用的代码如下,但这仍然会重复父节点
classes = [] #everyones id
for item in lst:
name = item['id']
if name not in classes:
classes.append(name)
treenodes = {}
root_node = None
for item in lst: # Create tree nodes
item['children'] = []
name = item['id']
treenodes[name] = item
parent = item['ManagerID']
if parent not in classes: # parent is root node, create
if parent not in treenodes:
node = {}
node['ManagerID'] = 0 #set manager to root
node['children'] = []
node['id'] = parent
root_node = node
treenodes[parent] = node
# Connect parents and children
for item in lst: # Create tree nodes
parent = item['ManagerID']
parent_node = treenodes[parent]
parent_node['children'].append(item)
output = treenodes
任何帮助都将不胜感激。
答案 0 :(得分:1)
您的代码实际上正在运行,但是您需要输入treenodes[0]
(CEO)。 treenodes
中其余的键值对仅用于簿记,以便轻松找到给定员工条目的给定经理。
如果您不能指望0
作为根节点的ID,那么您可以利用CEO被标记为自我管理这一事实;根节点是管理员ID指向自己的ID的节点。更为常见的情况是,根节点根本没有父ID。
您还将CEO添加到了他们自己的children
列表中(CEO的经理ID是他们自己的ID),因此您在树中有一个递归引用。
您找到的代码不是最清晰或最有效的。我会从id
到复制对象建立一个字典(因此您原来的lst
字典是不变的),然后遍历该结构并将条目添加到其经理ID条目中。我正在使用“根节点自引用”规则(因此管理器ID等于他们自己的ID):
employees = {}
managers = set()
root_id = None
for emp in lst:
id, mid = emp['id'], emp['ManagerID']
# create a copy of emp, and add a "children" list
employees[id] = {**emp, 'children': []}
managers.add(mid)
if id == mid:
# the root of the tree references itself as the manager
root_id = id
# add empty manager entries for missing manager IDs, reporting to root ID.
for id in managers - employees.keys():
employees[id] = {
'id': id, 'ManagerID': root_id, 'children': [],
'job': None, 'name': None
}
for id, emp in employees.items():
manager = employees[emp.pop('ManagerID')]
if id != root_id: # don't add the root to anything
manager['children'].append(emp)
output = employees[root_id]
上面的代码使用一个集合来跟踪看到过哪些经理ID,因此您可以轻松添加缺少的经理条目(在这种情况下,报告给CEO)。
您的输入将产生:
{'id': 0, 'job': 'CEO', 'name': 'John Smith', 'children':
[{'id': 1, 'job': 'Medical Manager', 'name': 'Medic 1', 'children':
[{'id': 2, 'job': 'Medical Assist', 'name': 'Medic 2', 'children': []}],
},
{'id': 3, 'job': 'ICT Manager', 'name': 'ICT 1', 'children':
[{'id': 4, 'job': 'ICT Assist', 'name': 'ICT 2', 'children':
[{'id': 5, 'job': 'ICT Junior', 'name': 'ICT 3', 'children': []}]
}]
}]
}
答案 1 :(得分:1)
这是用于构建层次结构的递归版本。
from pprint import pprint
def to_lookup(employees):
employee_lookup = dict()
for employee in employees:
if employee["id"] != employee["ManagerID"]:
manager_id = employee["ManagerID"]
children = employee_lookup.get(manager_id)
if not children:
children = employee_lookup[manager_id] = list()
children.append(employee.copy())
else:
manager = employee.copy()
return manager, employee_lookup
def build_hierarchy(manager, employee_lookup):
employees = employee_lookup.get(manager["id"], list())
for employee in employees:
build_hierarchy(employee, employee_lookup)
if employees:
manager['children'] = employees
return manager
employees = [
{"id": 0, "job": "CEO", "ManagerID": 0, "name": "John Smith"},
{"id": 1, "job": "Medical Manager", "ManagerID": 0, "name": "Medic 1"},
{"id": 2, "job": "Medical Assist", "ManagerID": 1, "name": "Medic 2"},
{"id": 3, "job": "ICT Manager", "ManagerID": 0, "name": "ICT 1"},
{"id": 4, "job": "ICT Assist", "ManagerID": 3, "name": "ICT 2"},
{"id": 5, "job": "ICT Junior", "ManagerID": 4, "name": "ICT 3"}
]
manager, employee_lookup = to_lookup(employees)
hierarchy = build_hierarchy(manager, employee_lookup)
pprint(hierarchy)
{'ManagerID': 0,
'children': [{'ManagerID': 0,
'children': [{'ManagerID': 1,
'id': 2,
'job': 'Medical Assist',
'name': 'Medic 2'}],
'id': 1,
'job': 'Medical Manager',
'name': 'Medic 1'},
{'ManagerID': 0,
'children': [{'ManagerID': 3,
'children': [{'ManagerID': 4,
'id': 5,
'job': 'ICT Junior',
'name': 'ICT 3'}],
'id': 4,
'job': 'ICT Assist',
'name': 'ICT 2'}],
'id': 3,
'job': 'ICT Manager',
'name': 'ICT 1'}],
'id': 0,
'job': 'CEO',
'name': 'John Smith'}
hierarchy_size = 2000000
employees = [
{"id": 0, "ManagerID": 0},
]
for idx in range(1, hierarchy_size):
manager_id = random.randint(0, idx - 1)
employees.append({"id": idx, "ManagerID": manager_id})
start = datetime.datetime.now()
manager, employee_lookup = to_lookup(employees)
hierarchy = build_hierarchy(manager, employee_lookup)
print(datetime.datetime.now() - start)