简体   繁体   English

使用while循环从函数返回变量

[英]Return variables from a function with a while loop

I am new to Python, thus apologize in advance! 我是Python的新手,因此提前道歉! I am trying to return two lists from a function with a while loop that reads an xml file. 我试图从一个函数返回两个列表,其中while循环读取一个xml文件。 I can't figure out how to do it. 我无法弄清楚该怎么做。 I am referring to imres (integer) and subres (2 integers) in the code below, that are found ~10 times in the loop. 我指的是下面代码中的imres(整数)和subres(2个整数),在循环中找到~10次。 Debuging shows that the variables are properly filled in the loop, but I don't know how to return the filled lists and I get the empty lists instead. Debuging显示变量在循环中正确填充,但我不知道如何返回填充列表,而是获取空列表。 Thanks. 谢谢。

def getresolution(node):
    imres = []
    subres = []
    child4 = node.firstChild
    while child4:
                ...
        for child8 in keepElementNodes(child7.childNodes):
            if child8.getAttribute('Hash:key') == 'ImageSize':
                X = float(child8.getElementsByTagName('Size:width')[0].firstChild.data)
                Y = float(child8.getElementsByTagName('Size:height')[0].firstChild.data)
                imres += [[X, Y]]
            if child8.getAttribute('Hash:key') == 'Resolution':
                subres += [int(child8.firstChild.data)]
        getresolution(child4)
        child4 = child4.nextSibling
    return [imres, subres]


[imres, subres] = getresolution(xml_file)

Not tested but this should point you in the right direction: 未经测试,但这应该指向正确的方向:

def getresolution(node):
    imres = []
    subres = []
    child4 = node.firstChild
    while child4:
                ...
        for child8 in keepElementNodes(child7.childNodes):
            if child8.getAttribute('Hash:key') == 'ImageSize':
                X = float(child8.getElementsByTagName('Size:width')[0].firstChild.data)
                Y = float(child8.getElementsByTagName('Size:height')[0].firstChild.data)
                imres += [[X, Y]]
                if child8.getAttribute('Hash:key') == 'Resolution':
                    subres += [int(child8.firstChild.data)]
        t_imres, t_subres = getresolution(child4)
        imres += t_imres
        subres += t_subres
        child4 = child4.nextSibling
    return [imres, subres]


[imres, subres] = getresolution(xml_file)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM