【问题标题】:Break loop in recursive function after finding variable python找到变量python后递归函数中的中断循环
【发布时间】:2020-10-14 16:29:09
【问题描述】:

我知道这个主题已经详细讨论过,但我的用例有些特殊。

问题描述:我正在尝试分析具有多个层的树。每层可能由元素组成,这些元素在访问时可能包含更多可扩展元素(列表)或只是一个简单元素(非列表)。基本上它就像 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”搜索变量后也继续运行。第一次找到这个变量的时候能停止代码就好了。

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
#

【问题讨论】:

  • 什么是“second_trial_function”?
  • 抱歉,应该是 search_function()。谢谢,我会更新问题。

标签: python list recursion


【解决方案1】:

'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

【讨论】:

    【解决方案2】:

    好的,感谢@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。也许有人可以澄清这一点。

    【讨论】:

      猜你喜欢
      • 2011-05-07
      • 1970-01-01
      • 1970-01-01
      • 2018-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多