python - 在python中创建一个嵌套列表
问题描述
根据下图,我有一棵树。我想使用python编写一个根据图片创建嵌套列表的函数。以下是我尝试过的,但它是错误的。有人可以帮忙吗? 在此处输入图像描述
def treepic (obj, x):
x=[]
x.append(obj.split)
if obj.lhs.is_leaf==False:
x.append([])
return treepic(obj.lhs,x[1]) #lhs:left child of the current node
else:
x.append(obj.lhs.split)
if obj.lhs.is_leaf==False:
x.append([])
return treepic(obj.rhs,x[2]) #rhs:right child of the current node
else:
x.append(obj.rhs.split)
return x
解决方案
推荐阅读
- kotlin - Lambda 表达式“非”谓词
- c++ - MFC C++ 应用程序:如何在任务管理器中清除命令行参数?
- javascript - Jest 什么时候无法导入包?
- jenkins - 为什么在 word 文件中更新日期和版本不起作用
- javascript - 完成一个项目:把它变成一个 .exe?
- html - 防止特定的 tr 改变表的大小
- string - 在字符串压缩中构建字符串结果
- python - 如何使用 for 循环和 if 语句检查 Jinja2 中的 dict 值是否为真
- php - 使用 laravel 和 jquery 访问音乐文件时出错
- swift - Xcode 模拟器上 cgImage 的 SpriteKit 问题,但设备上没有