更改循环的递归函数

时间:2014-08-25 18:40:07

标签: python loops python-2.7 optimization recursion

我已经读过,python中的递归工作非常慢。是否可以将此递归更改为循环函数,这将更快地工作? 我的功能有点复杂,但我会尝试展示最重要的部分:

class Element(object):
    def __init__(self, name):
        self.name = name
        self.priority = randint(1000)
    # some other operations and functions

import heapq

def fun(name):

      if condition():
         e = Element(name)
         # make some operations
         for i in e.sublist:
             if condition2():
                   heapq.heappush(heap,e)
             else:
                   updatepriority(e)
      if heap:
         top = heapq.heappop(heap)
         fun(top.name)

所以我有一个递归函数,它搜索抛出许多子列表并使用heapq模块构建优先级队列。 如果我有一个递归函数,例如计算Fibonnaci数,我可以很容易地将递归转换为循环。但在我的功能中,我没有回复声明,所以我不确定如何做到这一点。

2 个答案:

答案 0 :(得分:1)

我认为你可以直接这样做,实际上。只需先用root初始化堆,然后就可以使用while进行迭代。

def fun(root):
  # Initialize the heap with your root element.
  heap = []
  if condition():
    e = Element(root)
    # make some operations
    for i in e.sublist:
      if condition2():
        heapq.heappush(heap,e)
      else:
        updatepriority(e)

  # Now we can iterate through the remainder of the heap.
  while heap:
    top = heapq.heappop(heap)
    if condition():
      e = Element(top.name)
      # make some operations
      for i in e.sublist:
        if condition2():
          heapq.heappush(heap,e)
        else:
          updatepriority(e)

如果将实际的堆元素传递给函数参数而不是name,那么您可以立即将其推送到heap并跳过该初始步骤,使您的代码更简单。

def fun(root):
  heap = [root]

  # Now we can iterate through the remainder of the heap.
  while heap:
    top = heapq.heappop(heap)
    if condition():
      e = Element(top.name)
      # make some operations
      for i in e.sublist:
        if condition2():
          heapq.heappush(heap,e)
        else:
          updatepriority(e)

答案 1 :(得分:1)

是的,您始终可以将递归转换为迭代:

  • 首先确保所有递归调用都是尾调用,就像你所做的那样。

    def fun(name):
    
      if condition():
         e = new Element(name)
         # make some operations
         for i in e.sublist:
             if condition2():
                   heapq.heappush(heap,e)
             else:
                   updatepriority(e)
      if heap:
         top = heapq.heappop(heap)
         fun(top.name)
    
  • 然后让尾调用有自己的条件,if heap:将在代码中出现两次。

    def fun(name):
    
      if condition():
         e = new Element(name)
         # make some operations
         for i in e.sublist:
             if condition2():
                   heapq.heappush(heap,e)
             else:
                   updatepriority(e)
      if heap:
         top = heapq.heappop(heap)
      if heap:
         fun(top.name)
    
  • 现在换行do … while,删除尾调用并将条件谓词放入while中,并处理递归的参数。

    def fun(name):
      do #not python
    
         if condition():
             e = new Element(name)
             # make some operations
             for i in e.sublist:
                 if condition2():
                     heapq.heappush(heap,e)
                 else:
                     updatepriority(e)
          if heap:
             top = heapq.heappop(heap)
    
          name = top.name
      while heap # not python
    
  • 现在转换为while(python没有do ... while)

    def fun(name):
        heap=True
        while heap:
    
            if condition():
                e = new Element(name)
                # make some operations
                for i in e.sublist:
                    if condition2():
                        heapq.heappush(heap,e)
                    else:
                        updatepriority(e)
            if heap:
                top = heapq.heappop(heap)
    
            name = top.name
    

注意:创建top不需要是有条件的。 注意:我没有修复任何错误