从任意嵌套的xml树总和返回结果 [英] Return result from arbitrarily nested xml tree sum

查看:83
本文介绍了从任意嵌套的xml树总和返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following code that recurses(?) over an xml tree, which represents a simple equation:

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

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>

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

The code seems to work right up until the last loop, when it resets the numbers list and seems to start the process again, abortively.

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

How do I tell python (lxml) to stop when it has looked at every node? I've probably missed something important!

推荐答案

首先,我认为断言标签而不是可以使自己更容易>不是是某种东西(例如,尝试删除!=并替换为==).

First of all, I think you can make it easier for yourself by asserting that a tag is something, rather than it not being something (e.g. try to remove != and replace with ==).

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

One problem was the line addleafnodes(child) which returned something which then got thrown away. As you can get a list of numbers returned, which should be added/multiplied/etc., you can add these to the numbers list with numbers.extend(somelist). It is a bit hard to explain recursions, so perhaps if you take a look at the code it will make more sense. What I do sometimes, is add a depth variable to the function and increment it everytime I "recurse" - this way, when printing information, it may be easier to see which "level" a number is returned from and to where.

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个数字,因为它是一个二进制运算,然后依赖嵌套.显然,其他一元/二元/三元/..运算符也是如此.

Another thing: you've chosen to allow lists of numbers in an <add></add>. You could also consider it having simply 2 numbers, since it is a binary operation, and then rely on nesting. Same obviously applies for other unary/binary/ternary/.. operators.

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

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

That way, maybe you can eliminate the for-loop, but I'm not sure if it creates other problems. :-)

这篇关于从任意嵌套的xml树总和返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆