找到变量python后打破递归函数中的循环

时间:2020-10-14 16:29:09

标签: python list recursion

我知道已经对该主题进行了详细讨论,但是我的用例有些特殊。

问题描述:我正在尝试分析具有多层的树。每一层都可以由元素组成,这些元素在被访问时可能包含其他可扩展元素(列表),或者仅包含一个简单元素(非列表)。基本上就像Windows / Linux文件夹和文件系统。也许遵循以下架构将有助于更好地理解我指的是什么:

[Parent_Element]
|
+ [Element 1]
  |
  + [Subelement 1.1]
    + Subsubelement 1.1.1
    |
    + [hello_world]
      |
      Subsubsubelement 1.1.2.1
      |
      Subsubsubelement 1.1.2.2
    |
    + Subsubelement 1.1.3
  |
  + [Subelement 1.2]
    |
    + Subsubelement 1.2.1
  |  
  + Subelement 1.3
|
+ [Element 2]
  |  
  + Subelement2.1
|
+ Element 3

我尝试使用下面的代码,部分功能符合我的预期。问题是我的代码在找到所需的“ hello_world”的search_variable之后也保持运行。能够在第一次找到此变量时停止代码。

def search_function(parent_element, search_variable = "hello_world"):
    # Get elements of parent_element
    elements = parent_element.get_elements()
    #
    # Check if Parent_Element has subelements
    # TRUE: Parent_Element is LIST
    # FALSE: Parent_Element is not a LIST
    if isinstance(elements, list):
        #
        # Loop over each elem of the parent_element and 
        for elem in elements:
            # Check if elem has the desired name
            # TRUE: Save elem
            if elem.get_display_name().to_string() == search_variable:
                print("Search Successful!")
                return elem
                break
            #
            # FALSE: Keep on looking in the sub-elements
            else:
                search_function(elem)
                break
    else:
        pass
#

2 个答案:

答案 0 :(得分:0)

'break'将永远不会在您的代码中执行。您必须使用flag并将其设置为globalbreakreturn之后,这就是break从不执行的原因。

if elem.get_display_name().to_string() == search_variable:
    print("Search Successful!")
    return elem
    break

使用与上一个相同级别的另一个if语句来控制它。您可以像这样编辑代码。

           
        for elem in elements:
            # Check if elem has the desired name
            # TRUE: Save elem
            if elem.get_display_name().to_string() == search_variable:
                print("Search Successful!")
                flag = 1
                return elem

            #
            # FALSE: Keep on looking in the sub-elements
            else:
                second_trial_function(elem)
                break
    
            if flag : 
                break

答案 1 :(得分:0)

好吧,感谢@KittoMi的回应,我得以完成我的功能:

def search_function(input_node, search_variable = "hello_world"):
    global x
    global searched_elem
    #
    elements = input_node.get_children()
    #
    if isinstance(elements, list):
        for elem in elements:
            #
            print(elem)
            if elem.get_display_name().to_string() == search_variable:
                x = 1
                print("Found it!")
                print(elem)
                searched_elem = str(elem)
                return searched_elem
                #
            else:
                search_function(elem, search_variable=search_variable)
            if x:
                break
    else:
        pass
    #

但是我必须承认,我仍然不完全理解为什么没有 global searched_elem 它将无法正常工作。

该函数正在查找搜索变量,它打印了“找到它”消息,但随后又进行了2-3次计算,显示的结果是预期的结果。 这里是返回的print(elem)的一个小例子:

hello_world
Found!
hello_world
other_elem
yet_another_elem

这就是为什么我不得不求助于 global searched_elem 的原因。也许有人可以澄清这一点。

相关问题