簡體   English   中英

使用while循環從函數返回變量

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

我是Python的新手,因此提前道歉! 我試圖從一個函數返回兩個列表,其中while循環讀取一個xml文件。 我無法弄清楚該怎么做。 我指的是下面代碼中的imres(整數)和subres(2個整數),在循環中找到~10次。 Debuging顯示變量在循環中正確填充,但我不知道如何返回填充列表,而是獲取空列表。 謝謝。

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)

未經測試,但這應該指向正確的方向:

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