首页 > 解决方案 > 在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

标签: pythontreenested-lists

解决方案


推荐阅读