Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.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
返回最长单词的长度-Python_Python - Fatal编程技术网

返回最长单词的长度-Python

返回最长单词的长度-Python,python,Python,我正在学习Python课程,我被困在这个问题上: “编写一个名为length_of_longest_word的函数,该函数接受一个名为word_list的列表变量作为参数,并返回该列表中最长单词的长度。” 下面是我写的,感谢您的反馈 def length_of_longest_word(word_list): max_length = 0 for max_length in word_list: max_length = max(m

我正在学习Python课程,我被困在这个问题上:

“编写一个名为length_of_longest_word的函数,该函数接受一个名为word_list的列表变量作为参数,并返回该列表中最长单词的长度。”

下面是我写的,感谢您的反馈

    def length_of_longest_word(word_list):
        max_length = 0
        for max_length in word_list:
            max_length = max(max_length, length_of_longest_word)
        return max_length 


看看这个方法

def length_of_longest_word(word_list):
    max_length = 0
    for word in word_list:
        length = len(word)
        if length > max_length:
            max_length = length
    return length

你很接近。您必须在以下位置使用实际内置的
len

def length_of_longest_word(word_list):
    max_length = 0
    for word in word_list:
        max_length = max(max_length, len(word))
    return max_length
您也可以使用一些捷径,例如在较长的iterable上应用
max
,就像同时应用所有长度一样:

def length_of_longest_word(word_list):
    return max(map(len, word_list), default=0)

文档的一些链接:,

您可以将
max
与关键参数一起使用

max(map(len,word_list))
这相当于

max(len(word) for word in word_list)

这回答了你的问题吗?“感谢您的反馈”不是堆栈溢出问题。请重复介绍之旅。