Sto ricorsivamente attraverso un file xml, usando etree.Python: evita il loop annidato sull'array
import xml.etree.ElementTree as etree
tree = etree.parse('x.xml')
root = tree.getroot()
for child in root[0]:
for child in child.getchildren():
for child in child.getchildren():
for child in child.getchildren():
print(child.attrib)
qual è il modo idiomatico in python per evitare questi loop annidati.
getchildren() ⇒ list of Element instances [#]
Returns all subelements. The elements are returned in document order.
Returns:
A list of subelements.
Ho visto alcuni post nel SO come, Avoiding nested for loops ma non fa direttamente traducono in mio uso.
grazie.
'itertools.product' è un buon modo per evitare cicli annidati. Perché non si traduce nel tuo utilizzo? –
Vuoi specificamente gli attributi per gli elementi di 4 bambini profondi? – bogatron
scusa, non intendevo itertools.product non mi va bene, ma non potevo tradurre quell'esempio in matrici come nel mio caso. Non ho fatto molto Python, ma ci proverò. – bsr