Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
只有在Python3.x中添加另一个返回值时,我才能获得正确的输出_Python_Python 3.x - Fatal编程技术网

只有在Python3.x中添加另一个返回值时,我才能获得正确的输出

只有在Python3.x中添加另一个返回值时,我才能获得正确的输出,python,python-3.x,Python,Python 3.x,很抱歉这个不具体的标题,但我无法更好地解释它 我有以下python代码: def longestWord_with_recursion(wrds): if type(wrds) == str: return len(wrds),wrds return max([longestWord_with_recursion(x) for x in wrds]) lenwrds,wrds的回报给了我以下信息: (11, 'programming') #programmin

很抱歉这个不具体的标题,但我无法更好地解释它

我有以下python代码:

def longestWord_with_recursion(wrds):
    if type(wrds) == str:
        return len(wrds),wrds
    return max([longestWord_with_recursion(x) for x in wrds])

lenwrds,wrds的回报给了我以下信息:

(11, 'programming') #programming is the correct answer
you #another word in the list, but the wrong one
但是,因为我只想返回单词,所以我用return wrds替换了return,这给了我以下信息:

(11, 'programming') #programming is the correct answer
you #another word in the list, but the wrong one
为什么会发生这种情况?如果我添加另一个返回值,但如果我只返回这个值,它为什么会给出正确的单词?怎么才能解决这个问题呢

E:单词列表如下:

这句话的意思是:“你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你,你然后,“编程”、“必须”、“是”、“过程”、“of”、“放置”、“它们”、“in”、“埃德斯格”、“W”、“迪克斯特拉”]


当您仅返回单词时,使用递归语句形成的列表仅是单词列表。您是列表中按字母顺序排列的最后一个最大单词。您必须返回长度,才能使上一个调用级别对该数据进行操作

请注意,这不是递归,而是语法意义上的递归。您的函数有两个完全不同的操作,不会真正交互:如果用字符串调用,它只做一件事;如果用任何其他数据类型调用,它会迭代。这不是真正的基格和递归格,除非您有嵌套的单词列表。

尝试是:

def longestWord_with_recursion(wrds):
if type(wrds) == str:
    return len(wrds),wrds
return max([longestWord_with_recursion(x) for x in wrds])[1]

print(longestWord_with_recursion(words))

它将返回两个元素的列表,因此您只需指出您要打印的元素是第二个元素!

这似乎不是正确使用递归。这更像是您试图用递归函数重载最长的单词,该函数有两个功能:

将单词列表更改为具有单词长度、单词长度和 返回基于元组列表的最大单词。 您可以将整个函数重写为:

def longest_word(iterable):
    return max([(len(x), x) for x in iterable])
它还将返回最长的单词,同时仍使用内置的max函数。这将返回一个单词长度的元组word,因此如果您只想返回单词,可以执行以下操作:

def longest_word(iterable):
    return max([(len(x), x) for x in iterable])[1]
注意末尾的[1]

编辑:

查看max的文档以及@Kenny在评论中的评论,可以通过以下方式使其更加简单:

def longest_word(iterable):
    return max(iterable, key=len)

在这一点上,它真的值得成为它自己的函数吗?

提示:如果你不返回lenwrd,你的max函数会做什么?这不是使用递归做任何事情。max可以接受一个key函数。return maxwords,key=len查看我上面的评论;你可以告诉max使用key=len并跳过所有这些。是的,正如我看到的那样,我正在编辑它文档和您的评论。