python 数组转树形结构

原始数据

[{'_id': '65bf52cc7310663cb9d09665', 'category_name': '短视频', 'cover': 'https://wanderful.index.studio/wp-content/uploads/2021/11/[email protected]', 'parent_id': None, 'desc': '短视频', 'status': 1, 'create_time': '2024-02-04 17:03:08'}, {'_id': '65bf52e57310663cb9d09668', 'category_name': '萌宠', 'cover': 'https://wanderful.index.studio/wp-content/uploads/2021/11/[email protected]', 'parent_id': '65bf52cc7310663cb9d09665', 'desc': '萌宠', 'status': 1, 'create_time': '2024-02-04 17:03:33'}, {'_id': '65bf52ee7310663cb9d0966a', 'category_name': '美食', 'cover': 'https://wanderful.index.studio/wp-content/uploads/2021/11/[email protected]', 'parent_id': '65bf52cc7310663cb9d09665', 'desc': '美食', 'status': 1, 'create_time': '2024-02-04 17:03:42'}, {'_id': '65bf54517310663cb9d09679', 'category_name': '音乐', 'cover': 'https://wanderful.index.studio/wp-content/uploads/2021/11/[email protected]', 'parent_id': None, 'desc': 1, 'status': 1, 'create_time': '2024-02-04 17:09:37'}, {'_id': '65bf55447310663cb9d0967e', 'category_name': '乐器', 'cover': 'https://wanderful.index.studio/wp-content/uploads/2021/11/[email protected]', 'parent_id': '65bf54517310663cb9d09679', 'desc': None, 'status': 1, 'create_time': '2024-02-04 17:13:40'}, {'_id': '65bf558b7310663cb9d09682', 'category_name': '小吉他', 'cover': 'https://wanderful.index.studio/wp-content/uploads/2021/11/[email protected]', 'parent_id': '65bf54517310663cb9d09679', 'desc': '小吉他', 'status': 1, 'create_time': '2024-02-04 17:14:51'}, {'_id': '65bf57b27310663cb9d096a3', 'category_name': '晚会', 'cover': 'https://wanderful.index.studio/wp-content/uploads/2021/11/[email protected]', 'parent_id': None, 'desc': '晚会', 'status': 1, 'create_time': '2024-02-04 17:24:02'}, {'_id': '65bf57dc7310663cb9d096a7', 'category_name': '年欢晚会', 'cover': 'https://wanderful.index.studio/wp-content/uploads/2021/11/[email protected]', 'parent_id': '65bf57b27310663cb9d096a3', 'desc': '年欢晚会', 'status': 1, 'create_time': '2024-02-04 17:24:44'}, {'_id': '65bf57f47310663cb9d096ab', 'category_name': '跨年晚会', 'cover': 'https://wanderful.index.studio/wp-content/uploads/2021/11/[email protected]', 'parent_id': '65bf57b27310663cb9d096a3', 'desc': '跨年晚会', 'status': 1, 'create_time': '2024-02-04 17:25:08'}]
# 数组转树型结构
def array2tree(arr, parent_id=None):
    array = []
    for x in arr:
        if x["parent_id"] == parent_id:
            result = array2tree(arr, x["_id"])
            if result:
                x["children"] = result
            array.append(x)
    return array

转换后数据

[
		{
			"_id": "65bf52cc7310663cb9d09665",
			"category_name": "短视频",
			"cover": "https://wanderful.index.studio/wp-content/uploads/2021/11/[email protected]",
			"parent_id": null,
			"desc": "短视频",
			"status": 1,
			"create_time": "2024-02-04 17:03:08",
			"children": [
				{
					"_id": "65bf52e57310663cb9d09668",
					"category_name": "萌宠",
					"cover": "https://wanderful.index.studio/wp-content/uploads/2021/11/[email protected]",
					"parent_id": "65bf52cc7310663cb9d09665",
					"desc": "萌宠",
					"status": 1,
					"create_time": "2024-02-04 17:03:33"
				},
				{
					"_id": "65bf52ee7310663cb9d0966a",
					"category_name": "美食",
					"cover": "https://wanderful.index.studio/wp-content/uploads/2021/11/[email protected]",
					"parent_id": "65bf52cc7310663cb9d09665",
					"desc": "美食",
					"status": 1,
					"create_time": "2024-02-04 17:03:42"
				}
			]
		},
		{
			"_id": "65bf54517310663cb9d09679",
			"category_name": "音乐",
			"cover": "https://wanderful.index.studio/wp-content/uploads/2021/11/[email protected]",
			"parent_id": null,
			"desc": 1,
			"status": 1,
			"create_time": "2024-02-04 17:09:37",
			"children": [
				{
					"_id": "65bf55447310663cb9d0967e",
					"category_name": "乐器",
					"cover": "https://wanderful.index.studio/wp-content/uploads/2021/11/[email protected]",
					"parent_id": "65bf54517310663cb9d09679",
					"desc": null,
					"status": 1,
					"create_time": "2024-02-04 17:13:40"
				},
				{
					"_id": "65bf558b7310663cb9d09682",
					"category_name": "小吉他",
					"cover": "https://wanderful.index.studio/wp-content/uploads/2021/11/[email protected]",
					"parent_id": "65bf54517310663cb9d09679",
					"desc": "小吉他",
					"status": 1,
					"create_time": "2024-02-04 17:14:51"
				}
			]
		},
		{
			"_id": "65bf57b27310663cb9d096a3",
			"category_name": "晚会",
			"cover": "https://wanderful.index.studio/wp-content/uploads/2021/11/[email protected]",
			"parent_id": null,
			"desc": "晚会",
			"status": 1,
			"create_time": "2024-02-04 17:24:02",
			"children": [
				{
					"_id": "65bf57dc7310663cb9d096a7",
					"category_name": "年欢晚会",
					"cover": "https://wanderful.index.studio/wp-content/uploads/2021/11/[email protected]",
					"parent_id": "65bf57b27310663cb9d096a3",
					"desc": "年欢晚会",
					"status": 1,
					"create_time": "2024-02-04 17:24:44"
				},
				{
					"_id": "65bf57f47310663cb9d096ab",
					"category_name": "跨年晚会",
					"cover": "https://wanderful.index.studio/wp-content/uploads/2021/11/[email protected]",
					"parent_id": "65bf57b27310663cb9d096a3",
					"desc": "跨年晚会",
					"status": 1,
					"create_time": "2024-02-04 17:25:08"
				}
			]
		}
	],

你可能感兴趣的:(python,python,数据库,mysql)