关于递归函数的使用总结

import json
import copy

class Dept:

    def __init__(self, id, name, parentid) -> None:
        self.id = id
        self.name = name
        self.parentid = parentid
        self.children = []

    def __eq__(self, other):
        # if(self.id == other.id and self.name == other.name and self.parentid == other.parentid
        #    and self.children == other.children):
        if(self.__dict__ == other.__dict__):
            return True
        else:
            return False

  以上是一个组织的结构,现有一个list如下,里面存放的是这个组织的信息。求:将此list转换为树状结构

d1 = Dept('120', 'zhangsan', '12')
d2 = Dept('1200', 'lisi', '120')
d3 = Dept('1201', 'wangwu', '120')
d4 = Dept('12010', 'zhangsan', '1201')
l1 = [d4, d1, d3, d2 ]

  

def convert_list_to_tree(dept, list1):
    for i in list1:
        tmp = copy.deepcopy(dept)
        dept = traverse_dept(dept, i)
        if(tmp != dept):
            list1.remove(i)
            if(len(list1) == 0):
                return  dept
            else:
                dept = convert_list_to_tree(dept, list1)
            return dept
    # return dept

def traverse_dept(dept, d1):
    tmp = copy.deepcopy(d1)
    if(dept.id == d1.parentid):
        dept.children.append(d1)
    elif(d1.id == dept.parentid):
        d1.children.append(dept)
    else:
        for dept_tmp in dept.children:
            traverse_dept(dept_tmp, d1)
    return d1 if(tmp != d1) else dept

depts = convert_list_to_tree(l1[0], l1[1:])
for i in depts.children:
    print(i.__dict__)

  

import json
import copy

class Dept:

def __init__(self, id, name, parentid) -> None:
self.id = id
self.name = name
self.parentid = parentid
self.children = []

def __eq__(self, other):
# if(self.id == other.id and self.name == other.name and self.parentid == other.parentid
# and self.children == other.children):
if(self.__dict__ == other.__dict__):
return True
else:
return False

@staticmethod
def object_dept(d):
return Dept(d['id'], d['name'], d['parentid'])


class JsonObject:

def __init__(self, d):
self.__dict__ = d

d1 = Dept('120', 'zhangsan', '12')
d2 = Dept('1200', 'lisi', '120')
d3 = Dept('1201', 'wangwu', '120')
d4 = Dept('12010', 'zhangsan', '1201')
l1 = [d4, d1, d3, d2 ]

def convert_list_to_tree(dept, list1):
for i in list1:
tmp = copy.deepcopy(dept)
dept = traverse_dept(dept, i)
if(tmp != dept):
list1.remove(i)
if(len(list1) == 0):
return dept
else:
dept = convert_list_to_tree(dept, list1)
return dept
# return dept

def traverse_dept(dept, d1):
tmp = copy.deepcopy(d1)
if(dept.id == d1.parentid):
dept.children.append(d1)
elif(d1.id == dept.parentid):
d1.children.append(dept)
else:
for dept_tmp in dept.children:
traverse_dept(dept_tmp, d1)
return d1 if(tmp != d1) else dept

depts = convert_list_to_tree(l1[0], l1[1:])
for i in depts.children:
print(i.__dict__)
json1 = '{"id": "12010", "parentid": "1201", "children": [], "name": "zhangsan"}'
# dd = json.loads(json1, object_hook=Dept.object_dept)
dd = json.loads(json1, object_hook=JsonObject)
print(dd.name)
# print(dd.id)
# tmp = copy.deepcopy(d1)
# d1.children.append(d2)
# print(tmp==d1)

d1 = Dept('120', 'zhangsan', '12')
d11 = Dept('120', 'zhangsan', '12')
d13 = Dept('120', 'zhangsan', '12')
d14 = Dept('120', 'zhangsan', '12')
d1.children.append(d3)
d11.children.append(d4)
print(d1 == d11)

猜你喜欢

转载自www.cnblogs.com/yahutiaotiao/p/12716238.html