【问题标题】:Stuck on recursion陷入递归
【发布时间】:2014-06-23 23:16:07
【问题描述】:

我正在尝试编写一个带有 2 个参数、一个 int 和一个列表的函数。我必须将列表中的每个元素与 int 进行比较,并将它们存储在一个由 2 个列表组成的元组中,一个大于数字的列表和一个小于数字的列表。我正在尝试以递归方式执行此操作,但我不知道如何存储这些值,以便在使用递归时不会删除它们。

def greater_less(v:int,l:list):
    if l == []:
       return ([],[]) 
    elif l[0] > v:
       more = [l[0]] 
       return more + greater_less(v,l[1:])
    elif l[0] < v:
       less = [l[0]]
       return less + greater_less(v,l[1:]) 

问题是...当l == [] 时,一切都被清除了。另外,当我递归调用我的函数时,我相信之前的所有内容也都被清除了

练习递归以便提示如何解决我的递归问题会很棒

【问题讨论】:

  • 为什么要递归?有必要吗
  • 如果元素和a一样呢
  • 可能duplicate.

标签: python python-3.x recursion tuples


【解决方案1】:

让我们写一些代码:

def greater_less(v, l):
    # First, are we in the base case?
    if not l:
        return [], []

    # Second, do the recursive step
    smaller, greater = greater_less(v, l[1:])

    # Now, we also have l[0] to insert into these lists.
    if l[0] < v:
        smaller.insert(0, l[0])
    elif l[0] > v:
        greater.insert(0, l[0])
    else:
        pass

    # Finally, return these lists
    return smaller, greater

请注意,我们存储递归调用返回的列表,添加到正确的列表,然后返回。


让我们看一下这段代码的运行。为了减少重复,我将标记函数 AD 中的 4 段代码。所以 A 将是基本情况检查 (if not l...) 而 C 将是 if l[0] &lt; v ... else: pass 代码。

main() calls greater_less(2, [1,2,3])
     A: We are not in the base case because l has 3 elements.
     B: Recursive call of greater_less(2, [2, 3])
         A: We are not in the base case because l has 2 elements.
         B: Recursive call of greater_less(2, [3])
              A: We are not in the base case because l has 1 element.
              B: Recursive call of greater_less(2, [])
                  A: We __are__ in the base case because l has 0 elements.
                     Therefore, we won't reach B, C, or D of this call.
                     We return [], [].
              B: Recursive call returns. We have smaller = [], greater = []
              C: l[0] is 3 which is greater than 2.
                 Therefore, we prepend onto greater.
              D: Return smaller = [], greater = [3]
         B: Caller returns. We have smaller = [], greater = [3]
         C: l[0] is 2, which is equal to 2.
            So we don't prepend this number to either list.
         D: return smaller = [], greater = [3]
     B: Caller returns. We have smaller = [], greater = [3]
     C: l[0] is 1, which is less than 2.
        So prepend to the smaller list.
     D: Return smaller = [1], greater = [3]
main's call to greater_less() now returns with ([1], [3])

【讨论】:

  • 我有点不明白为什么你在 if/elif/else 之前做了递归步骤
  • 我不确定有什么好的方法来解释这一点。对我来说,上面的代码比在条件中进行调用更清晰。也许您应该使用 2 元素列表逐步完成递归调用。
  • 所以第一次运行它,我们在到达条件之前递归调用该函数。递归调用函数后返回smaller,greater,然后我们再次递归调用该函数,但是每次递归调用函数时,smaller,greater是如何更新自己而不是被清除的呢?通常我会看到一个 __ + 递归调用,但这里没有
  • 我认为您没有正确完成该功能。我试图包括一个这样做的例子,其中缩进代表递归。也许会有所帮助。
  • 几乎明白了。你知道我怎么能颠倒元组中列表的顺序吗?通过简单地添加 list.reverse() 不会做我想要的,因为当我们递归调用函数时,我们会不断地添加到列表中。我一直试图在 VERY END 处颠倒列表顺序
猜你喜欢
  • 2015-03-11
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 2017-12-05
  • 1970-01-01
  • 2016-03-15
  • 1970-01-01
相关资源
最近更新 更多