如何递归应用此功能

时间:2020-03-20 23:25:33

标签: python python-3.x

因此我有了此功能aux,这很容易理解。这个功能是什么 的确是看到有多少个数字大于a的第一个元素 列表。

list1 = [5,2,7,4,3,8]

def aux(list1):
    x=list1[0]
    res=1
    for number in range(1,len(list1)):
        if(list1[number]>x):
            res+=1
    return res

现在我想制作另一个函数,该函数递归使用aux 列表中的每个元素,看看谁返回的数字更大。但是我 没有看到我该如何递归地做到这一点。

3 个答案:

答案 0 :(得分:2)

现在我想制作另一个函数,该函数递归地对列表的每个元素使用aux,看看谁返回的数字更大。

我了解您可能需要以专门的递归方式解决此问题,但我认为没有必要。列表中使用函数逻辑返回最大数字的元素就是最小数字。可以通过min()函数找到。然后,您可以返回列表的长度减去一个的长度(如果您假设没有重复项),或者返回列表的长度减去另一个仅包含最低项目的列表的长度(应该有重复项)。

# Assums no duplicates    
def something(l):
    return (min(l), len(l)-1)

# Accounts for duplicates 
def betterthing(l):
    low = min(l)
    return (low, len(l) - len([x for x in l if x == low]))

l1 = [5,2,7,4,3,8]
print("{} is lowest & {} other items are larger.".format(*something(l1)))

l2 = [5,2,7,4,3,8,2,9,10,8]
print("{} is lowest & {} other items are larger.".format(*betterthing(l2)))

Example code in Python Tutor

编辑:

此函数的作用是查看比列表的第一个元素大多少个数字。

这似乎是一种不准确的函数描述方式。自将res变量初始化为1以来,它实际上返回的数字大于列表中第一个元素加1的数量。

答案 1 :(得分:0)

这对我有用:

list1 = [5,2,7,4,3,8]

def aux(list1,i,res,x):
    if (i+1)>len(list1):
        print('end')
        print(res)
    else:
        if list1[i]>x:
            print('element '+str(i)+' greater than first element')
            res+=1
        aux(list1,i+1,res,x) #recursion happens here


aux(list1,i,1,list1[0])

答案 2 :(得分:0)

我知道这不是问题的一部分,但是我会这样重写函数:

def count_gt(x, container):
    greater_than_x = 0
    for number in container:
        if number > x:
            greater_than_x += 1
    return greater_than_x

让我们看一下一些递归示例,以使其变得更容易。

列表总数:

def get_sum(container):
    if container == []:
        return 0
    else:
        return container[0] + get_sum(container[1:])

使用循环,可以轻松解决问题:

greatest = 0
for number in numbers:
    greatest = max(count_gt_first(number), greatest)

如果您的任务是在正数列表中找到最大数,则可以这样递归表示:

def get_max(container):
    if len(container) == 1:
        return container[0]
    else:
        return max(container[0], get_max(container[1:]))

例如:

get_max([4, 3, 9, 1])
    --> max(4, get_max[3, 9, 1])
    --> max(5, max(3, get_max[9, 1]))
    --> max(5, max(3, max(9, get_max[1])))
    --> max(5, max(3, max(9, 1)))
    <-- max(5, max(3, 9))
    <-- max(5, 9)
    <-- 9

现在,您可以修改函数以找到最大值,而不是函数的最大值。

def get_max(container):
    return _get_max(container, container)

def _get_max(container, original_container):
    if len(container) == 1:
        return container
    else:
        result_for_this = count_gt(container[0], container)
        candidate_for_rest = _get_max(container[1:], container)
        result_for_rest = count_gt(candidate_for_rest, container)
        if result_for_this > result_for_rest:
            return container[0]
        else:
            return candidate_for_rest

您可以重写该函数以使用 tail recursion

def get_max(container):
    return _get_max(container, container, container[0])

def _get_max(container, original_container, max_candidate):
    if count_gt(container[0], container) > count_gt(max_candidate, container):
        max_candidate = container[0]
    if len(container) == 1:
        return max_candidate
    return _get_max(container[1:], original_container, max_candidate)

阅读有关递归的更多信息:

https://realpython.com/python-thinking-recursively/

https://chrispenner.ca/posts/python-tail-recursion