栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 递归

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 递归

为啥要写,因为用到了,不看不行了,那就看看呗

看了17分钟,给我的感觉就是 判断逻辑正常走,然后return自己

我的目标呢就是获取最儿子层的id,所以这么写,,

代码如下

# -*- coding: utf-8 -*-
# @create_time : 2022/7/14 下午8:55
# @Author : CY
# @File : xxx.py
a = [
  {
    "id": "1547510177782173696",
    "parentId": "0",
    "weight": 1,
    "name": "区域名称",
    "children": [
      {
        "id": "1547511157533839360",
        "parentId": "1547510177782173696",
        "weight": 2,
        "name": "11",
        "children": [
          {
            "id": "1547511182317981696",
            "parentId": "1547511157533839360",
            "weight": 3,
            "name": "11-11",
            "children": [
              {
                "id": "1547511210352709632",
                "parentId": "1547511182317981696",
                "weight": 4,
                "name": "11-11-22",
                "children": [
                  {
                    "id": "1547511264534728704",
                    "parentId": "1547511210352709632",
                    "weight": 5,
                    "name": "11-11-22-11"
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]


def get_children_data(list_data):
    ch_data = list_data[0].get('children', None)
    if ch_data is None: return list_data[0].get('id')
    return get_children_data(ch_data)


if __name__ == '__main__':
    cc = get_children_data(a)
    print(cc)

 

暑期编程PK赛 得CSDN机械键盘等精美礼品!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1015416.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号