在 Python 中打印决策树的递归函数:抑制“无”

发布于 2024-12-19 10:58:10 字数 775 浏览 1 评论 0原文

我将决策树作为字典在 Python 中实现。示例:

sampletree = {'spl':'foo', 'go_r':{'cut':150} ,  'l':{'val':100}, 'r':{'val':200}}

我有一个递归函数打印树:

def TREE_PRINT(tree, indent=''):
    #is this a leaf node?
    if 'val' in tree:
        print str(tree['val'])
    else:
        #print the criteria
        print 'split: '+ str(tree['spl']) + ' ' + str(tree['go_r'])
        #print the branches
        print indent+'L->', TREE_PRINT(tree['l'], indent+'  ')
        print indent+'R->', TREE_PRINT(tree['r'], indent+'  ')

如何抑制运行该函数时打印的 None ?

TREE_PRINT(sampletree)
split: foo {'cut': 150}
L-> 100
None
R-> 200
None

我尝试返回 '',但随后出现了不需要的额外换行符。 我正在基于《集体智能编程》第 151 页中的“printtree”函数进行构建。

I decision trees implemented in Python as dictionaries. Example:

sampletree = {'spl':'foo', 'go_r':{'cut':150} ,  'l':{'val':100}, 'r':{'val':200}}

I have a recursive function prints the tree:

def TREE_PRINT(tree, indent=''):
    #is this a leaf node?
    if 'val' in tree:
        print str(tree['val'])
    else:
        #print the criteria
        print 'split: '+ str(tree['spl']) + ' ' + str(tree['go_r'])
        #print the branches
        print indent+'L->', TREE_PRINT(tree['l'], indent+'  ')
        print indent+'R->', TREE_PRINT(tree['r'], indent+'  ')

How do I suppress the None's that are printed when I run the function?

TREE_PRINT(sampletree)
split: foo {'cut': 150}
L-> 100
None
R-> 200
None

I tried returning '', but then I get line unwanted extra line breaks.
I'm building off of the 'printtree' function from page 151 in Programming Collective Intelligence.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒路情人 2024-12-26 10:58:10

你的函数的返回值是None。不要打印函数的返回值 - 只需调用您的函数。

def TREE_PRINT(tree, indent=''):
    #is this a leaf node?
    if 'val' in tree:
        print str(tree['val'])
    else:
        #print the criteria
        print 'split: '+ str(tree['spl']) + ' ' + str(tree['go_r'])
        #print the branches
        print indent+'L->',
        TREE_PRINT(tree['l'], indent+'  ')

        print indent+'R->',
        TREE_PRINT(tree['r'], indent+'  ')

结果

split: foo {'cut': 150}
L-> 100
R-> 200

查看它在线运行:ideone

The return value of your function is None. Don't print the return value of your function - just call your function.

def TREE_PRINT(tree, indent=''):
    #is this a leaf node?
    if 'val' in tree:
        print str(tree['val'])
    else:
        #print the criteria
        print 'split: '+ str(tree['spl']) + ' ' + str(tree['go_r'])
        #print the branches
        print indent+'L->',
        TREE_PRINT(tree['l'], indent+'  ')

        print indent+'R->',
        TREE_PRINT(tree['r'], indent+'  ')

Result

split: foo {'cut': 150}
L-> 100
R-> 200

See it working online: ideone

梦年海沫深 2024-12-26 10:58:10

您需要决定 TREE_PRINT 是打印字符串表示形式还是返回它。如果您的意思是它应该打印数据,那么您想要的代码是:

def TREE_PRINT(tree, indent=''):
    #is this a leaf node?
    if 'val' in tree:
        print str(tree['val'])
    else:
        #print the criteria
        print 'split: '+ str(tree['spl']) + ' ' + str(tree['go_r'])
        #print the branches
        print indent+'L->',
        TREE_PRINT(tree['l'], indent+'  ')
        print indent+'R->',
        TREE_PRINT(tree['r'], indent+'  ')

You need to decide if TREE_PRINT prints the string representation or returns it. If you mean that it should print the data, then what you want your code to be is:

def TREE_PRINT(tree, indent=''):
    #is this a leaf node?
    if 'val' in tree:
        print str(tree['val'])
    else:
        #print the criteria
        print 'split: '+ str(tree['spl']) + ' ' + str(tree['go_r'])
        #print the branches
        print indent+'L->',
        TREE_PRINT(tree['l'], indent+'  ')
        print indent+'R->',
        TREE_PRINT(tree['r'], indent+'  ')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文