List 用Python显示列表中所有最长的单词

List 用Python显示列表中所有最长的单词,list,function,for-loop,max,min,List,Function,For Loop,Max,Min,我想显示列表中最长的所有单词。我使用了max函数,但是max函数只返回列表中最大字符串的第一个,即“have”。如何使其打印出字符串的所有最长元素 理想的输出:“已”为“良好” 获得的输出:“have” def longestWord(input_str): input_list = input_str.split() return max(input_list, key=len) longestWord("I have been good") output: 'have'

我想显示列表中最长的所有单词。我使用了max函数,但是max函数只返回列表中最大字符串的第一个,即“have”。如何使其打印出字符串的所有最长元素

理想的输出:“已”为“良好” 获得的输出:“have”

def longestWord(input_str):
    input_list = input_str.split()
    return max(input_list, key=len)

longestWord("I have been good")

output: 'have'

尝试以下代码,将所有项目的len与max len进行比较,然后将其附加到另一个列表中

def longestWord(input_str):
input_list = input_str.split()
lenght = len(max(input_list, key=len))
allMax=[]
for f in input_list:
    if len(f) == lenght:
       allMax.append(f)
       print(f)
return allMax       
最长的词(“我一直很好”)