通过while循环从函数返回变量

时间:2019-04-16 13:02:42

标签: python list while-loop return assign

我是Python的新手,因此提前致歉!我试图从一个带有读取XML文件的while循环的函数返回两个列表。我不知道该怎么做。我指的是下面代码中的imres(整数)和subres(2个整数),它们在循环中被发现约10次。调试显示变量已正确填充到循环中,但是我不知道如何返回填充列表,而是获取了空列表。谢谢。

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)

1 个答案:

答案 0 :(得分:0)

未经测试,但这应该为您指明正确的方向:

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)