从任意嵌套的xml树总和返回结果

时间:2019-06-04 15:21:56

标签: python-3.x recursion lxml

我有以下代码在xml树上递归(?),它表示一个简单的方程:

root = etree.XML(request.data ['expression'])

def addleafnodes(root):
    numbers = []
    for child in root:
        if root.tag != "root" and root.tag != "expression":
            print(root.tag, child.text)

            if child.tag != "add" and child.tag != "multiply":
                numbers.append(int(child.text))
                print("NUMBERS", numbers)
            elif child.tag == "add":
                numbers.append(np.sum(addleafnodes(child)))
                print("NUMBERS", numbers)
            elif child.tag == "multiply":
                numbers.append(np.prod(addleafnodes(child)))
                print("NUMBERS", numbers)
        print("NUMBERS", numbers)
        addleafnodes(child)
    return numbers

newresults = addleafnodes(root)
print("[NEW RESULTS]", newresults)

该xml是:

<root>
    <expression>
        <add>
            <add>
                <number>1</number>
                <number>2</number>
            </add>
            <multiply>
                <number>2</number>
                <number>3</number>
            </multiply>
            <add>
                <number>4</number>
                <number>5</number>
            </add>
            <number>3</number>
            <multiply>
                <number>1</number>
                <add>
                    <number>3</number>
                    <number>4</number>
                </add>
            </multiply>
        </add>
    </expression>
</root>

该代码似乎可以一直工作到最后一个循环,当它重置数字列表并似乎再次异常终止地开始该过程时。

当它查看每个节点时,如何告诉python(lxml)停止运行?我可能错过了一些重要的东西!

1 个答案:

答案 0 :(得分:1)

首先,我认为您可以通过声明标签而不是 not 来简化自己(例如,尝试删除! =并替换为==)。

一个问题是行addleafnodes(child)返回的内容随后被丢弃。当您获得返回的数字列表(应加/乘/等)后,可以使用numbers将它们添加到numbers.extend(somelist)列表中。解释递归有点困难,所以也许如果您看一下代码,它将更有意义。有时,我要做的是在函数中添加一个depth变量,并在每次“递归”时将其递增-这样,在打印信息时,可能更容易看到一个数字从哪个“级别”返回到另一个“级别”。在哪里。

def addleafnodes(root):
    numbers = []
    for child in root:
        if child.tag == "number":
            numbers.append(int(child.text))
        elif child.tag == "add":
            numbers.append(np.sum(addleafnodes(child)))
        elif child.tag == "multiply":
            numbers.append(np.prod(addleafnodes(child)))
        else:
            numbers.extend(addleafnodes(child))
        print("NUMBERS: ", numbers)
    return numbers

newresults = addleafnodes(root)
print("[NEW RESULTS]", newresults)

# outputs:
NUMBERS:  [1]
NUMBERS:  [1, 2]
NUMBERS:  [3]
NUMBERS:  [2]
NUMBERS:  [2, 3]
NUMBERS:  [3, 6]
NUMBERS:  [4]
NUMBERS:  [4, 5]
NUMBERS:  [3, 6, 9]
NUMBERS:  [3, 6, 9, 3]
NUMBERS:  [1]
NUMBERS:  [3]
NUMBERS:  [3, 4]
NUMBERS:  [1, 7]
NUMBERS:  [3, 6, 9, 3, 7]
NUMBERS:  [28]
NUMBERS:  [28]
[NEW RESULTS] [28]

另一件事:您已选择允许<add></add>中的数字列表。您也可以认为它只有2个数字,因为它是二进制运算,然后依赖嵌套。显然,其他一元/二进制/三元/ ..运算符也是如此。

<add>
    <number>1</number>
    <add>
        <number>2</number>
        <number>3</number>
    </add>
</add>

那样,也许您可​​以消除for循环,但是我不确定它是否会引起其他问题。 :-)