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)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值