2013-02-08 10 views
5

पर नेस्टेड लूप से बचें मैं एट्री का उपयोग करके एक एक्सएमएल फ़ाइल के माध्यम से रिकर्स कर रहा हूं।पायथन: सरणी

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) 

इन घोंसले के लिए इन घोंसले से बचने के लिए पाइथन में बेवकूफ तरीका क्या है।

getchildren() ⇒ list of Element instances [#] 
    Returns all subelements. The elements are returned in document order. 

Returns: 
A list of subelements. 

मैं की तरह अतः में, Avoiding nested for loops कुछ पोस्ट को देखा लेकिन सीधे अपने प्रयोग के लिए अनुवाद नहीं करता है।

धन्यवाद।

+1

'itertools.product' नेस्टेड लूप से बचने का एक अच्छा तरीका है। यह आपके उपयोग में अनुवाद क्यों नहीं करता है? –

+0

क्या आप विशेष रूप से तत्व 4 बच्चों के लिए विशेषताओं के गुण चाहते हैं? – bogatron

+0

क्षमा करें, मेरा मतलब यह नहीं था कि itertools.product मेरे अनुरूप नहीं है, लेकिन मेरे मामले में ऐसे सरणी के लिए उस उदाहरण का अनुवाद नहीं कर सका। मैंने बहुत पाइथन नहीं किया है, लेकिन कोशिश करेंगे। – bsr

उत्तर

3

आप बच्चों कि पेड़ में गहरी n स्तर हैं, और फिर उन के माध्यम से पुनरावृति प्राप्त करना चाहते हैं, तो आप कर सकते हैं:

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 

फिर, तत्वों चार स्तरों गहरी पाने के लिए, आप बस होगा कहते हैं:

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

या, आप पत्र-गांठ (यानी नोड्स किसी भी बच्चों को खुद की जरूरत नहीं है कि) के सभी प्राप्त करना चाहते हैं, तो आप कर सकते हैं:

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

itertools.chain.from_iterable घोंसले के एक स्तर को चपटा देगा; आप functools.reduce उपयोग कर सकते हैं यह n बार लागू करने के लिए (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) 

ध्यान दें कि getchildren पदावनत है; एक नोड को फिर से अपने बच्चों को सीधे पैदा करता है।

संबंधित मुद्दे