【问题标题】:Recursive function returning none in Python [duplicate]Python中没有返回的递归函数[重复]
【发布时间】:2013-10-13 10:53:47
【问题描述】:

我有这段代码,由于某种原因,当我尝试返回路径时,我得到了None

def get_path(dictionary, rqfile, prefix=[]):        
    for filename in dictionary.keys():
        path = prefix + [filename]
        if not isinstance(dictionary[filename], dict):          
            if rqfile in str(os.path.join(*path)):
                return str(os.path.join(*path))
        else:
            get_path(directory[filename], rqfile, path)

有没有办法解决这个问题?

【问题讨论】:

    标签: python recursion


    【解决方案1】:

    虽然我认为 Martijn Pieters 的回答解决了他回答中的主要问题(您需要从递归案例中返回),但我认为他建议的代码不会正常工作。

    您正在尝试对嵌套的 dictionary 字典中的 rqfile 值进行深度优先搜索。但是您当前的代码不能正确处理递归情况。如果在其递归调用之一中找到结果,或者递归调用未能找到目标,则它需要做出适当的响应。

    以下是我认为您需要的,为了清晰起见,对某些内容进行了重命名或重新排列:

    def get_path(directory, rqfile, prefix=[]):
        for filename, value in directory.items():
            path_list = prefix + [filename]
            if not isinstance(value, dict): # base case
                path = os.path.join(*path_list)
                if rqfile in path:   # Found the file. Do you want to do something
                    return path      # with the value here, or is it junk?
    
            else: # recursive case
                try:
                    return get_path(value, rqfile, path_list) # this only returns if 
                except ValueError:                     # the recursion doesn't raise
                    pass
    
        raise ValueError("Requested file not found") # not found here or in children
    

    示例用法:

    >>> directory = {"a": "a info",
                     "b": {"c": "b/c info", "d": "b/d info"},
                     "e": {"f": "e/f info", "g": {"h": "e/g/h info"}}}
    >>> print(get_path(directory, "h"))
    e\g\h
    >>> print(get_path(directory, r'g\h'))
    e\g\h
    

    如果您不想在找不到文件时引发异常,您还可以返回一个类似 None 的标记值来代替最后一行,并在递归情况下检查标记值而不是try/except:

     result = get_path(value, rqfile, path)
     if result is not None:
         return result
    

    【讨论】:

      【解决方案2】:

      需要返回递归结果:

      else:
         return get_path(directory[filename], rqfile, path)
      

      否则函数在执行该语句后简单地结束,导致返回None

      您可能希望删除 else: 并始终在最后返回:

      for filename in dictionary.keys():
          path = prefix+[filename]
          if not isinstance(dictionary[filename], dict):
      
              if rqfile in str(os.path.join(*path)):
                  return str(os.path.join(*path))
      
          return get_path(directory[filename], rqfile, path)
      

      因为如果rqfile in str(os.path.join(*path))False,那么你也可以在没有return 的情况下结束你的函数。如果在这种情况下递归不是正确的选择,但返回 None 不是正确的,那么您也需要处理这种边缘情况。

      【讨论】:

      • 您的主要观点(关于返回递归调用)是正确的,但我不确定您关于摆脱 else 的建议是否正确。原始代码有一个分支(其中dictionary[filename] 不是字典,但rqfile 不是连接路径的子字符串),它将继续循环遍历更多文件名。但是,可能确实需要在函数末尾有一个单独的 return 语句,以捕获没有匹配的文件名。在这种情况下引发异常可能是合适的(然后您需要在递归周围使用try/catch)。
      • @Blckknght:我当然希望 OP 考虑一下没有达到return 的可能性。
      猜你喜欢
      • 2021-01-09
      • 2020-08-24
      • 2022-08-13
      • 2017-06-19
      • 2019-07-15
      相关资源
      最近更新 更多