如何打印所有叶子

2024-09-27 21:26:42 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的密码:

from stat_parser import Parser, display_tree


parser = Parser()

# http://www.thrivenotes.com/the-last-question/
evaluate =  parser.parse("""


In it's first elections, Aam Aadmi Party beats Sheila Dixit winning the Delhi Elections.


""")


for i in range(0,len(evaluate)):
    if evaluate[i].node == 'S':
        for j in evaluate[i]:
            for l in j:
                print l.leaves
        print

但是,这会返回:

<bound method Tree.leaves of Tree(u'PRP', ['it'])>
<bound method Tree.leaves of Tree(u'VBZ', ["'s"])>
<bound method Tree.leaves of Tree(u'NP', [Tree(u'JJ', ['first']), Tree(u'NNS', ['elections'])])>

<bound method Tree.leaves of Tree(u'NNP', ['Aam'])>
<bound method Tree.leaves of Tree(u'NNP', ['Aadmi'])>
<bound method Tree.leaves of Tree(u'NNP', ['Party'])>
<bound method Tree.leaves of Tree(u'NNP', ['beats'])>
<bound method Tree.leaves of Tree(u'NNP', ['Sheila'])>
<bound method Tree.leaves of Tree(u'NNP', ['Dixit'])>
<bound method Tree.leaves of Tree(u'VBG', ['winning'])>
<bound method Tree.leaves of Tree(u'NP', [Tree(u'DT', ['the']), Tree(u'NNP', ['Delhi']), Tree(u'NNP', ['Elections'])])>

但是我想按正确的顺序打印“S”分支下的所有内容?你知道吗

我该怎么做?你知道吗


Tags: oftheintreeparserforitmethod

热门问题