简体   繁体   English

递归函数如何返回列表的元组?

[英]How does a recursive function returns a tuple of lists?

Question is: Define a recursive function named separate; 问题是:定义一个名为分离的递归函数; it is passed a predicate and a list; 它被传递一个谓词和一个列表; it returns a 2-tuple whose 0 index is a list of all the values in the argument list for which the predicate returns True, and whose 1 index is a list of all the values in the argument list for which the predicate returns False. 它返回一个2元组,其0索引是谓词为其返回谓词的参数列表中所有值的列表,而其1索引是谓词为其返回False的参数列表中所有值的列表。 The call separate(predicate.is_positive,[1,-3,-2,4,0,-1,8]) returns ([1,4,8], [-3,-2,0,-1]) . separate(predicate.is_positive,[1,-3,-2,4,0,-1,8])调用separate(predicate.is_positive,[1,-3,-2,4,0,-1,8])返回([1,4,8], [-3,-2,0,-1]) Note 0 is not positive. 注意0不是正数。 Hint: like the fast version of the power function in the notes, you can define and bind (but not rebind) a local name or can write a nested function (like square in power) to help with the computation. 提示:就像注释中幂函数的快速版本一样,您可以定义和绑定(但不能重新绑定)本地名称,也可以编写嵌套函数(如幂平方)来帮助计算。

Here is the example of his power function: 这是他的幂函数的示例:

def power(a,n):
    def square(n) : n*n
    if n == 0:
        return 1
    else:
       if n%2 == 1:
           return a*power(a,n-1)
       else:
           return square( power(a,n//2) )

My attempt: 我的尝试:

def separate(p,l):
    l1=[]
    l2=[]
    if l == []:
        return [],[]
    else:
        if p(l[0]):
            l1=([l[0]]+map_pos(p,l[1:]))
            return l1,l2
        else:
            l2.extend([l[0]]+separate(p,l[1:]))
            return l1,l2

calling this function: print(predicate.is_positive,[1, -3, -2, 4, 0, -1, 8]) will gives me: TypeError: can only concatenate list (not "tuple") to list 调用此函数: print(predicate.is_positive,[1, -3, -2, 4, 0, -1, 8]) TypeError: can only concatenate list (not "tuple") to list print(predicate.is_positive,[1, -3, -2, 4, 0, -1, 8])将给我: TypeError: can only concatenate list (not "tuple") to list

Note predicate.is_positive is a function from predicate module which takes an int and return True if int is positive. 注意predicate.is_positive是谓词模块中的一个函数,该函数接受一个int,如果int为正,则返回True。

Can someone please help me with this? 有人可以帮我吗? With actual code will be nice really appreciated. 有了实际的代码,将不胜感激。

This may be whay you are attempting to do 这可能是您尝试做的事情

def separate(p, L):
    if L == []:
        return [], []

    l1, l2 = separate(p, L[1:])

    item = L[0]

    if p(item):
        l1.append(item)
    else:
        l2.append(item)
    return l1, l2    

It's not very efficient due to the L[1:] which creates a new list for each item 由于L[1:]会为每个项目创建一个新列表,因此效率不是很高

you can use a default argument to avoid making the slices 您可以使用默认参数来避免切片

def separate(p, L, idx=0):
    if idx == len(L):
        return [], []

    l1, l2 = separate(p, L, idx + 1)

    item = L[idx]

    if p(item):
        l1.append(item)
    else:
        l2.append(item)
    return l1, l2    

It still looks clumsy. 它看起来仍然很笨拙。 It's not really a task that calls for a recursive solution 这并不是真正需要递归解决方案的任务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM