Skip to content Skip to sidebar Skip to footer

Python: Avoid Nested Loop On Array

I am recursing through an xml file, using etree. import xml.etree.ElementTree as etree tree = etree.parse('x.xml') root = tree.getroot() for child in root[0]: for child in child.g

Solution 1:

If you want to get the children that are n levels deep in the tree, and then iterate through them, you can do:

def childrenAtLevel(tree, n):
    if n == 1:
        for child in tree.getchildren():
            yield child
    else:
        for child in tree.getchildren():
            for e in childrenAtLevel(child, n-1):
                yield e

Then, to get the elements four levels deep, you would simply say:

for e in childrenAtLevel(root, 4):
     # do something with e

Or, if you want to get all of the leaf nodes (i.e. the nodes that don't have any children themselves), you can do:

def getLeafNodes(tree):
    if len(tree) == 0:
         yield tree
    else:
         for child in tree.getchildren():
            for leaf in getLeafNodes(child):
                yield leaf

Solution 2:

itertools.chain.from_iterable will flatten one level of nesting; you can use functools.reduce to apply it n times (Compressing "n"-time object member call):

from itertools import chain
from functools import reduce

for child in reduce(lambda x, _: chain.from_iterable(x), range(3), root):
    print(child.attrib)

Note that getchildren is deprecated; iterating a node yields its children directly.


Post a Comment for "Python: Avoid Nested Loop On Array"