简体   繁体   English

在列表中查找最大值的无意外值 - Python 3递归

[英]Unexpected Value of None for Finding Maximum in a List - Python 3 Recursion

I am trying to write a simple recursive function to find the maximum in a list without using any built in functions, with the exceptions being 'print' and 'len'. 我试图编写一个简单的递归函数来查找列表中的最大值而不使用任何内置函数,例外的是'print'和'len'。 I made a simple linear search using recursion that compares each member of the list to the current Max. 我使用递归进行了简单的线性搜索,将列表的每个成员与当前的Max进行比较。

x=[1,2,3]
Max=x[-1]

def Max_list(Max, x, c=2):

    if len(x)==1:
        return Max

    else:

        if c==(len(x)+1):
            print('hi')
            return Max

        elif x[len(x)-c]>Max:
            Max=x[len(x)-c]
            c+=1
            Max_list(Max, x, c)

        elif x[len(x)-c]<=Max:
            c+=1
            Max_list(Max, x, c)

print(Max_list(Max, x))

What confuses me is that my program prints 'hi' (this was a verification that my if condition was satisfied), but returns None. 令我困惑的是我的程序打印'hi'(这是验证我的if条件是否满足),但返回None。 I could have made it attempt to return anything, and it would still return 'None'. 我本可以尝试返回任何东西,它仍会返回'无'。 I am wondering how to fix it obviously, but if anyone could give me an explanation as to why my code will always return None in its current state, that would be wonderful. 我想知道如何解决这个问题,但是如果有人能给我一个解释,为什么我的代码总是会在当前状态下返回None,那就太好了。

As one of the users pointed out in the comments, you need to add a return statement to the two calls to Max_list(Max, x, c) . 正如注释中指出的用户之一,您需要为两个Max_list(Max, x, c)调用Max_list(Max, x, c)添加一个return语句。 Why is this the case? 为什么会这样?

In Python, if you have call a function that has no return statement, the default return value is None . 在Python中,如果调用的函数没有return语句,则默认返回值为None If we look at your recursion, what it's actually doing is this: 如果我们看看你的递归,它实际上做的是:

    Initial Call:
    Max_list(3, x, 3)
              vvvvv
            Max_list(3, x, 4)
                      vvvvv
                     print('hi')
                     return 3 
            return None
    return None 

Upon adding those return statements, we get something in the vein of: 在添加这些返回语句后,我们得到了以下内容:

    Initial Call:
    Max_list(3, x, 3)
               vvvvvv
            Max_list(3, x, 4)
                      vvvvv
                     print('hi')
                     return 3 
            return the value of the above (3)
    return the value of the above (3)

As user2357112 said, you must explicity use return . 正如user2357112所说,你必须明确使用return Also Patrick Gallagher does make a good point about using x[-1] to retrieve the last element in the list. Patrick Gallagher也确实使用x[-1]来检索列表中的最后一个元素。

x=[1,2,3]
Max=x[-1]

def Max_list(Max, x, c=2):

    if len(x)==1:
        return Max

    else:

        if c==(len(x)+1):
            return Max

        elif x[len(x)-c]>Max:
           Max=x[len(x)-c]
            c+=1
            return Max_list(Max, x, c)

        elif x[len(x)-c]<=Max:
            c+=1
            return Max_list(Max, x, c)

print(Max_list(Max, x))

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

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