Return 来自带有 while 循环的函数的变量
Return variables from a function with a while loop
我是Python的新手,所以提前道歉!我正在尝试从一个函数中 return 两个列表,该函数带有读取 xml 文件的 while 循环。我不知道该怎么做。我指的是下面代码中的 imres(整数)和 subres(2 个整数),它们在循环中出现了约 10 次。调试显示变量已正确填充到循环中,但我不知道如何 return 填充列表,而是得到空列表。谢谢。
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)
我是Python的新手,所以提前道歉!我正在尝试从一个函数中 return 两个列表,该函数带有读取 xml 文件的 while 循环。我不知道该怎么做。我指的是下面代码中的 imres(整数)和 subres(2 个整数),它们在循环中出现了约 10 次。调试显示变量已正确填充到循环中,但我不知道如何 return 填充列表,而是得到空列表。谢谢。
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)