Python 如何在列表中找到最长的单词
我试过这个:Python 如何在列表中找到最长的单词,python,list,Python,List,我试过这个: sentence = input("Sentence without punctuation: ") lst = [sentence] longestString = max(lst, key=len) print(longestString) print(len(longestString)) sentence = input("Sentence without punctuation: ") lst = [sentence]
sentence = input("Sentence without punctuation: ")
lst = [sentence]
longestString = max(lst, key=len)
print(longestString)
print(len(longestString))
sentence = input("Sentence without punctuation: ")
lst = [sentence]
longestString = max(lst, key=len)
print(longestString)
print(len(longestString))
但我的输出是:
sentence = input("Sentence without punctuation: ")
lst = [sentence]
longestString = max(lst, key=len)
print(longestString)
print(len(longestString))
sentence = input("Sentence without punctuation: ")
lst = [sentence]
longestString = max(lst, key=len)
print(longestString)
print(len(longestString))
如果你想把你的句子分解成单词,而不是这个
lst = [sentence]
你反而想要
lst = sentence.split()
否则,您将创建一个包含单个元素的列表,该元素是您的整个字符串。只需将
lst=[句子]
替换为lst=句子.split()
您提供了代码作为输出…这是否回答了您的问题?