码迷,mamicode.com
首页 > 其他好文 > 详细

递归分类和树形式分类

时间:2020-06-20 16:14:58      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:contain   for循环   key   列表   not   level   厦门   ade   child   

无限极分类实现

数据准备

data=[
    {"cat_id":1,"name":"安徽","parent_id":0},
    {"cat_id":2,"name":"福建","parent_id":0},
    {"cat_id":3,"name":"阜阳","parent_id":1},
    {"cat_id":4,"name":"亳州","parent_id":1},
    {"cat_id":5,"name":"马鞍山","parent_id":1},
    {"cat_id":6,"name":"临泉","parent_id":3},
    {"cat_id":7,"name":"阜南","parent_id":3},
    {"cat_id":8,"name":"界首","parent_id":3},
    {"cat_id":9,"name":"太和","parent_id":3},
    {"cat_id":10,"name":"涡阳","parent_id":4},
    {"cat_id":11,"name":"利辛","parent_id":4},
    {"cat_id":12,"name":"花山区","parent_id":5},
    {"cat_id":13,"name":"雨山区","parent_id":5},
    {"cat_id":14,"name":"厦门","parent_id":2},
    {"cat_id":15,"name":"莆田","parent_id":2},
    {"cat_id":16,"name":"湖里区","parent_id":14},
    {"cat_id":17,"name":"思明区","parent_id":14},
    {"cat_id":18,"name":"思明区","parent_id":14},
    {"cat_id":19,"name":"集美区","parent_id":14},
    {"cat_id":20,"name":"同安区","parent_id":14},
    {"cat_id":21,"name":"翔安区","parent_id":14},
    {"cat_id":22,"name":"仙游县","parent_id":15},
    {"cat_id":23,"name":"莆田市区","parent_id":15},
    {"cat_id":24,"name":"庙岔镇","parent_id":6},
    {"cat_id":25,"name":"滑集镇","parent_id":6},
    {"cat_id":26,"name":"集美街道","parent_id":19},
    {"cat_id":27,"name":"马巷","parent_id":21},
]

无限极递归分类

每查找到一个子集,就会递归调用自身,去找自己的子集,重新使用for循环遍历一次;知道没有子集,开始回溯,效率低下。分类好的数据全部排序在一张列表,没有具体区分归属,使用时相当不便。

res=[]
def get_son(data,level=0,parent_id=0,is_clear=True):
    if is_clear:
        res.clear()
    for item in data:
        if item[‘parent_id‘]==parent_id:
            item[‘level‘]=level
            res.append(item)
            get_son(data,level=level+1,parent_id=item[‘cat_id‘],is_clear=False)
    return res
 
print(get_son(data))

运行结果:

‘‘‘
[
{‘cat_id‘: 1, ‘name‘: ‘安徽‘, ‘parent_id‘: 0, ‘level‘: 0},
{‘cat_id‘: 3, ‘name‘: ‘阜阳‘, ‘parent_id‘: 1, ‘level‘: 1},
{‘cat_id‘: 6, ‘name‘: ‘临泉‘, ‘parent_id‘: 3, ‘level‘: 2}, 
{‘cat_id‘: 24, ‘name‘: ‘庙岔镇‘, ‘parent_id‘: 6, ‘level‘: 3}, 
{‘cat_id‘: 25, ‘name‘: ‘滑集镇‘, ‘parent_id‘: 6, ‘level‘: 3}, 
{‘cat_id‘: 7, ‘name‘: ‘阜南‘, ‘parent_id‘: 3, ‘level‘: 2},
{‘cat_id‘: 8, ‘name‘: ‘界首‘, ‘parent_id‘: 3, ‘level‘: 2},
{‘cat_id‘: 9, ‘name‘: ‘太和‘, ‘parent_id‘: 3, ‘level‘: 2},
{‘cat_id‘: 4, ‘name‘: ‘亳州‘, ‘parent_id‘: 1, ‘level‘: 1},
{‘cat_id‘: 10, ‘name‘: ‘涡阳‘, ‘parent_id‘: 4, ‘level‘: 2}, 
{‘cat_id‘: 11, ‘name‘: ‘利辛‘, ‘parent_id‘: 4, ‘level‘: 2}, 
{‘cat_id‘: 5, ‘name‘: ‘马鞍山‘, ‘parent_id‘: 1, ‘level‘: 1},
{‘cat_id‘: 12, ‘name‘: ‘花山区‘, ‘parent_id‘: 5, ‘level‘: 2},
{‘cat_id‘: 13, ‘name‘: ‘雨山区‘, ‘parent_id‘: 5, ‘level‘: 2},
{‘cat_id‘: 2, ‘name‘: ‘福建‘, ‘parent_id‘: 0, ‘level‘: 0}, 
{‘cat_id‘: 14, ‘name‘: ‘厦门‘, ‘parent_id‘: 2, ‘level‘: 1}, 
{‘cat_id‘: 16, ‘name‘: ‘湖里区‘, ‘parent_id‘: 14, ‘level‘: 2},
{‘cat_id‘: 17, ‘name‘: ‘思明区‘, ‘parent_id‘: 14, ‘level‘: 2},
{‘cat_id‘: 18, ‘name‘: ‘思明区‘, ‘parent_id‘: 14, ‘level‘: 2},
{‘cat_id‘: 19, ‘name‘: ‘集美区‘, ‘parent_id‘: 14, ‘level‘: 2},
{‘cat_id‘: 26, ‘name‘: ‘集美街道‘, ‘parent_id‘: 19, ‘level‘: 3},
{‘cat_id‘: 20, ‘name‘: ‘同安区‘, ‘parent_id‘: 14, ‘level‘: 2}, 
{‘cat_id‘: 21, ‘name‘: ‘翔安区‘, ‘parent_id‘: 14, ‘level‘: 2}, 
{‘cat_id‘: 27, ‘name‘: ‘马巷‘, ‘parent_id‘: 21, ‘level‘: 3},
{‘cat_id‘: 15, ‘name‘: ‘莆田‘, ‘parent_id‘: 2, ‘level‘: 1},
{‘cat_id‘: 22, ‘name‘: ‘仙游县‘, ‘parent_id‘: 15, ‘level‘: 2},
 {‘cat_id‘: 23, ‘name‘: ‘莆田市区‘, ‘parent_id‘: 15, ‘level‘: 2}
 ]
‘‘‘

无限极树形式的分类

两次for循环:

第一次根据每个字典中的分类id(cat_id),重新根据key(cat_id) =value(原字典)的形式组装并放到新列表(tree)中, ;

第二次for循环,将所有子分类添加到所属的父分类中

def get_tree(data):
    lists=[]
    tree={}
    for i in data:
        # 将原data字典中每个字典根据每个字典中的cat_id作为键,每个字典作为值,添加到定义的tree字典中。
        tree[i[‘cat_id‘]]=i
    print(tree)
    for item in data:
        # 如果item中的parent_id 为0 ,添加到lists中
        if not item[‘parent_id‘]:
            lists.append(tree[item[‘cat_id‘]])
        else:
            # for循环出来的item的父亲字典中(通过parent_id判断他所属的父亲)如果没有children这个键
            if "children" not in tree[item[‘parent_id‘]]:
                # 在tree的子字典中添加一个键值对,键是children,值是空列表
                tree[item[‘parent_id‘]][‘children‘]=[]
            # 如果有children这个键,直接根据parent_id判断其所属的父亲字典,并添加到父亲字典的children对应的列表中。
            tree[item[‘parent_id‘]][‘children‘].append(tree[item[‘cat_id‘]])  # # tree[item[‘cat_id‘]] = item
    return lists

print(get_tree(data))

打印结果:

print(tree)


‘‘‘
{
1: {‘cat_id‘: 1, ‘name‘: ‘安徽‘, ‘parent_id‘: 0},
2: {‘cat_id‘: 2, ‘name‘: ‘福建‘, ‘parent_id‘: 0}, 
3: {‘cat_id‘: 3, ‘name‘: ‘阜阳‘, ‘parent_id‘: 1}, 
4: {‘cat_id‘: 4, ‘name‘: ‘亳州‘, ‘parent_id‘: 1}, 
5: {‘cat_id‘: 5, ‘name‘: ‘马鞍山‘, ‘parent_id‘: 1}, 
6: {‘cat_id‘: 6, ‘name‘: ‘临泉‘, ‘parent_id‘: 3}, 
7: {‘cat_id‘: 7, ‘name‘: ‘阜南‘, ‘parent_id‘: 3}, 
8: {‘cat_id‘: 8, ‘name‘: ‘界首‘, ‘parent_id‘: 3},
9: {‘cat_id‘: 9, ‘name‘: ‘太和‘, ‘parent_id‘: 3}, 
10: {‘cat_id‘: 10, ‘name‘: ‘涡阳‘, ‘parent_id‘: 4},
11: {‘cat_id‘: 11, ‘name‘: ‘利辛‘, ‘parent_id‘: 4},
12: {‘cat_id‘: 12, ‘name‘: ‘花山区‘, ‘parent_id‘: 5},
13: {‘cat_id‘: 13, ‘name‘: ‘雨山区‘, ‘parent_id‘: 5},
14: {‘cat_id‘: 14, ‘name‘: ‘厦门‘, ‘parent_id‘: 2}, 
15: {‘cat_id‘: 15, ‘name‘: ‘莆田‘, ‘parent_id‘: 2}, 
16: {‘cat_id‘: 16, ‘name‘: ‘湖里区‘, ‘parent_id‘: 14},
17: {‘cat_id‘: 17, ‘name‘: ‘思明区‘, ‘parent_id‘: 14},
18: {‘cat_id‘: 18, ‘name‘: ‘思明区‘, ‘parent_id‘: 14}, 
19: {‘cat_id‘: 19, ‘name‘: ‘集美区‘, ‘parent_id‘: 14}, 
20: {‘cat_id‘: 20, ‘name‘: ‘同安区‘, ‘parent_id‘: 14}, 
21: {‘cat_id‘: 21, ‘name‘: ‘翔安区‘, ‘parent_id‘: 14},
22: {‘cat_id‘: 22, ‘name‘: ‘仙游县‘, ‘parent_id‘: 15}, 
23: {‘cat_id‘: 23, ‘name‘: ‘莆田市区‘, ‘parent_id‘: 15}, 
24: {‘cat_id‘: 24, ‘name‘: ‘庙岔镇‘, ‘parent_id‘: 6},
25: {‘cat_id‘: 25, ‘name‘: ‘滑集镇‘, ‘parent_id‘: 6}, 
26: {‘cat_id‘: 26, ‘name‘: ‘集美街道‘, ‘parent_id‘: 19},
27: {‘cat_id‘: 27, ‘name‘: ‘马巷‘, ‘parent_id‘: 21},
}

‘‘‘

print(get_tree(data))

‘‘‘
[{‘cat_id‘: 1, ‘name‘: ‘安徽‘, ‘parent_id‘: 0, 
‘children‘: [{‘cat_id‘: 3, ‘name‘: ‘阜阳‘, ‘parent_id‘: 1, 
‘children‘: [{‘cat_id‘: 6, ‘name‘: ‘临泉‘, ‘parent_id‘: 3, 
‘children‘: [{‘cat_id‘: 24, ‘name‘: ‘庙岔镇‘, ‘parent_id‘: 6}, 
{‘cat_id‘: 25, ‘name‘: ‘滑集镇‘, ‘parent_id‘: 6}]}, 

{‘cat_id‘: 7, ‘name‘: ‘阜南‘, ‘parent_id‘: 3}, 
{‘cat_id‘: 8, ‘name‘: ‘界首‘, ‘parent_id‘: 3}, 
{‘cat_id‘: 9, ‘name‘: ‘太和‘, ‘parent_id‘: 3}]}, 

{‘cat_id‘: 4, ‘name‘: ‘亳州‘, ‘parent_id‘: 1,
‘children‘: [{‘cat_id‘: 10, ‘name‘: ‘涡阳‘, ‘parent_id‘: 4}, 
{‘cat_id‘: 11, ‘name‘: ‘利辛‘, ‘parent_id‘: 4}]}, 
{‘cat_id‘: 5, ‘name‘: ‘马鞍山‘, ‘parent_id‘: 1, 
‘children‘: [{‘cat_id‘: 12, ‘name‘: ‘花山区‘, ‘parent_id‘: 5}, 
{‘cat_id‘: 13, ‘name‘: ‘雨山区‘, ‘parent_id‘: 5}]}]}, 

{‘cat_id‘: 2, ‘name‘: ‘福建‘, ‘parent_id‘: 0,
‘children‘: [{‘cat_id‘: 14, ‘name‘: ‘厦门‘, ‘parent_id‘: 2,
‘children‘: [{‘cat_id‘: 16, ‘name‘: ‘湖里区‘, ‘parent_id‘: 14},
{‘cat_id‘: 17, ‘name‘: ‘思明区‘, ‘parent_id‘: 14}, 
{‘cat_id‘: 18, ‘name‘: ‘思明区‘, ‘parent_id‘: 14}, 
{‘cat_id‘: 19, ‘name‘: ‘集美区‘, ‘parent_id‘: 14,
‘children‘: [{‘cat_id‘: 26, ‘name‘: ‘集美街道‘, ‘parent_id‘: 19}]},

{‘cat_id‘: 20, ‘name‘: ‘同安区‘, ‘parent_id‘: 14},
{‘cat_id‘: 21, ‘name‘: ‘翔安区‘, ‘parent_id‘: 14, 
‘children‘: [{‘cat_id‘: 27, ‘name‘: ‘马巷‘, ‘parent_id‘: 21}]}]},
{‘cat_id‘: 15, ‘name‘: ‘莆田‘, ‘parent_id‘: 2, 
‘children‘: [{‘cat_id‘: 22, ‘name‘: ‘仙游县‘, 
‘parent_id‘: 15}, {‘cat_id‘: 23, ‘name‘: ‘莆田市区‘, ‘parent_id‘: 15}]}]}]

‘‘‘

递归分类和树形式分类

标签:contain   for循环   key   列表   not   level   厦门   ade   child   

原文地址:https://www.cnblogs.com/zhangchaocoming/p/13168879.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!