为什么我的 python 函数返回多个括号?

发布于 2024-12-16 16:18:40 字数 303 浏览 1 评论 0原文

我对 python 有点菜鸟,但我正在尝试创建一个递归函数,它的工作方式就像内置的 range 函数:

def Range (lo, hi):
    if lo >= hi:
        return []
    else:
        return [lo, Range (lo+1,hi)]

但它返回多个列表。

它返回的是 [3,[4,[5,[6,[]]]]][3,4,5,6]代码> 这是为什么?我该如何解决?

I'm somewhat of a noob to python but I'm trying to create a recursive function which works just like the built in range function:

def Range (lo, hi):
    if lo >= hi:
        return []
    else:
        return [lo, Range (lo+1,hi)]

but its returning multiple lists.

Instead of [3,4,5,6], which is what I want, its returning [3,[4,[5,[6,[]]]]]
Why is this and how do I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空城缀染半城烟沙 2024-12-23 16:18:40

当你像这样递归时,Range每次都会返回一个列表:

Range(3,7)
# translates to
[3, Range(4,7)]
# which translates to
[3, [4, Range(5,7)]]
# etc.

为了避免这种情况,请将列表添加在一起:

def Range (lo, hi):
    if lo >= hi:
        return []
    else:
        return [lo] + Range(lo+1, hi)

编辑:

正如@delnan指出的,这个函数是< em>非常效率低下 - 它在没有尾部调用优化的语言中递归*并且它生成两个(可能三个)每个递归级别的新列表。 @mipadi 的答案性能更高,因为它只创建一个列表(accaccumulator 参数)并在递归时传递它。

* 这可能对于 Python 语言来说并不正确,但我 99% 确信对于最常见的 Python 实现(即 CPython)来说这是正确的。

When you recurse like that, Range returns a list each time:

Range(3,7)
# translates to
[3, Range(4,7)]
# which translates to
[3, [4, Range(5,7)]]
# etc.

In order to avoid this, add your lists together:

def Range (lo, hi):
    if lo >= hi:
        return []
    else:
        return [lo] + Range(lo+1, hi)

EDIT:

As @delnan points out, this function is very inefficient - it both recurses in a language without tail-call optimization* and it generates two (possibly three) new lists for each level of recursion. @mipadi's answer is more performant because it creates only one list (the acc or accumulator argument) and passes it as it recurses.

* This may not be true for the Python language, but I'm 99% sure it is true for the most common implementation of Python, namely CPython.

忘你却要生生世世 2024-12-23 16:18:40

您的 Range 函数返回一个列表,因此在最后一行中您将返回列表中的列表。您可能应该做的是维护一个累加器并向其添加值:

def Range(lo, hi, acc=None):
    if acc is None:
        acc = []
    if lo >= hi:
        return acc
    else:
        acc.append(lo)
        return Range(lo+1, hi, acc)

Your Range function returns a list, so in your last line you are returning a list within a list. What you probably should do is maintain an accumulator and add values to that:

def Range(lo, hi, acc=None):
    if acc is None:
        acc = []
    if lo >= hi:
        return acc
    else:
        acc.append(lo)
        return Range(lo+1, hi, acc)
烙印 2024-12-23 16:18:40
def Range (lo, hi):
    if lo >= hi:
        return []
    else:
        return [lo] + Range (lo+1, hi)

但你可能会得到 StackOverflow

def Range (lo, hi):
    if lo >= hi:
        return []
    else:
        return [lo] + Range (lo+1, hi)

but you might get StackOverflow

谈下烟灰 2024-12-23 16:18:40

Range 中的每次递归都会返回一个列表,它是上一次递归的列表中的第二个元素。当然 Python 有一个内置函数,但是如果你想自己构建它,你可能只想以

return [lo] + Range(lo+1, hi)

Each recursion into Range returns a list, which is the second element in the list for the previous recursion. Of course Python has a built-in function for this, but if you want to build it yourself, you probably just want to end with

return [lo] + Range(lo+1, hi)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文