For loop 嵌套for循环,类型为';lxml.etree.ElementDepthFirstIterator';蟒蛇3.4

For loop 嵌套for循环,类型为';lxml.etree.ElementDepthFirstIterator';蟒蛇3.4,for-loop,python-3.4,For Loop,Python 3.4,我正在使用Python3.4,试图在xml文件中找到类似的子树 这是我的代码: def find_unique_sub_tree(oneFile, resultPath, list_ignored_att): root = find_root(oneFile) idNodes = root.iter('xmlID') for n1 in idNodes: id1 = n1.attrib['idName'] for n2

我正在使用Python3.4,试图在xml文件中找到类似的子树

这是我的代码:

def find_unique_sub_tree(oneFile, resultPath, list_ignored_att):
      root = find_root(oneFile)
      idNodes = root.iter('xmlID')
      for n1 in idNodes: 
          id1 = n1.attrib['idName']
          for n2 in idNodes:
              id2 = n2.attrib['idName']
              do something
嵌套for循环的问题在于,它只从外部循环获取第一项,当从内部循环完成时,它不会返回到从外部循环获取第二项。我理解这个问题,但解决不了。 我尝试了以下解决方案:

idNodes = root.iter('xmlID')
idNodes2 = root.iter('xmlID')
for n1 in idNodes: 
      id1 = n1.attrib['idName']
      for n2 in idNodes2:
我也试过:

root = find_root(oneFile)
root2 = find_root(oneFile)
idNodes = root.iter('xmlID')
idNodes2 = root.iter('xmlID')
for n1 in idNodes: 
      id1 = n1.attrib['idName']
      for n2 in idNodes2:
这些并没有解决问题

最后,我尝试了一个深度复制,但它给出了错误:

root = find_root(oneFile)
idNodes = root.iter('xmlID')
idNodes2 = copy.deepcopy(idNodes)
for n1 in idNodes: 
      id1 = n1.attrib['idName']
      for n2 in idNodes2:
所以我试了一下:

idNodes = list(root.iter('xmlID'))
idNodes2 = copy.deepcopy(idNodes)
for n1 in idNodes: 
   id1 = n1.attrib['idName']
   for n2 in idNodes2:
它消除了错误,但没有解决问题

有人能帮忙吗?
谢谢

我不确定我是否完全理解了这个问题,但是您最好列出所有子节点,并使用不同的函数使用所需的任何标准来比较列表中的每个元素:

node_list = list()
for n1 in idNodes:
    node_list.append(n1.attrib['idName'])
x = 1
for idName in node_list:
    for idName2 in node_list[x:]
        result = compare_nodes(idName, idName2)
        x += 1