Python:在列表中查找最长/最短单词并在函数中调用它们
我有一个单词列表: 单词=[“阿尔法”、“欧米茄”、“向上”、“向下”、“上方”、“下方”、“紫色”、“红色”、“蓝色”、“绿色”] 我有两个函数用于查找列表中最短和最长的单词:Python:在列表中查找最长/最短单词并在函数中调用它们,python,function,loops,for-loop,maps,Python,Function,Loops,For Loop,Maps,我有一个单词列表: 单词=[“阿尔法”、“欧米茄”、“向上”、“向下”、“上方”、“下方”、“紫色”、“红色”、“蓝色”、“绿色”] 我有两个函数用于查找列表中最短和最长的单词: def bigWords(list=[], *args): largestWord="" largestLen=0 for word in list: if largestWord<len(word): largestWord=len(word)
def bigWords(list=[], *args):
largestWord=""
largestLen=0
for word in list:
if largestWord<len(word):
largestWord=len(word)
largestWord=word
print "The longest word(s) in the list is %s." % largestWord
def smallWords(list=[], *args):
smallestWord=""
smallestLen=0
for word in list:
if smallestLen>len(word):
smallestLen>len(word)
smallestWord=word
print "The shortest word(s) in the list is: %s." % (smallestWord)
这只是返回,而不输入列表中的单词:
The longest word(s) in the list is .
The longest word(s) in the list is .
The longest word(s) in the list is .
The longest word(s) in the list is .
The longest word(s) in the list is .
The longest word(s) in the list is .
The longest word(s) in the list is .
The longest word(s) in the list is .
The longest word(s) in the list is .
The longest word(s) in the list is .
The shortest word(s) in the list is: .
The shortest word(s) in the list is: .
The shortest word(s) in the list is: .
The shortest word(s) in the list is: .
The shortest word(s) in the list is: .
The shortest word(s) in the list is: .
The shortest word(s) in the list is: .
The shortest word(s) in the list is: .
The shortest word(s) in the list is: .
The shortest word(s) in the list is: .
不知道为什么,非常感谢您的帮助。首先,您的函数代码
bigWords
中有一个错误。你应该和长度而不是单词进行比较,如下所示
def bigWords(list=[], *args):
largestWord=""
largestLen=0
for word in list:
if largestLen<len(word):
largestLen=len(word)
largestWord=word
print "The longest word(s) in the list is %s." % largestWord
如果您愿意,有更简单的方法来解决问题:
words=["alpha","omega","up","down","over","under","purple","red","blue","green"]
sortedwords = sorted(words, key=len)
print "The number of words in the list is: %s." % (len(words),)
print "The shortest word in the list is: %s." % (sortedwords[0],)
print "The longest word in the list is: %s." % (sortedwords[-1],)
这将产生:
The number of words in the list is: 10.
The shortest word in the list is: up.
The longest word in the list is: purple.
你是如此接近-但我认为问题在于
callFunctions()
。您正在将func_list2
中的函数映射到words数组中的每个字符串,而不是将函数作为一个整体应用到数组。使用map是个好主意,它是一个强大的函数,但您不需要在这里使用它。下面是我用测试工具测试的代码。试试看。祝你学习/做的项目好运
def bigWords(list=[], *args):
largestWord=""
for word in list:
if len(largestWord)<len(word):
largestWord= word
print "The longest word(s) in the list is %s." % largestWord
return largestWord
def smallWords(list=[], *args):
smallestWord = bigWords(list)
for word in list:
if len(smallestWord)> len(word):
smallestWord = word
print "The shortest word(s) in the list is: %s." % (smallestWord)
def callFunctions():
###Words###
words=["alpha","omega","up","down","over","under","purple","red","blue","green"]
wordLength=len(words)
print "The amount of words[] is %d" % wordLength
func_list2 = [bigWords, smallWords]
for f in func_list2:
f(words)
callFunctions()
def bigWords(列表=[],*args):
largestWord=“”
对于列表中的单词:
如果len(最大单词)len(单词):
最小的词
打印“列表中最短的单词是:%s.”%(最短的单词)
def callFunctions():
###言语###
单词=[“阿尔法”、“欧米茄”、“向上”、“向下”、“上方”、“下方”、“紫色”、“红色”、“蓝色”、“绿色”]
字长=len(字)
打印“字数[]为%d”%wordLength
func_list2=[bigWords,smallWords]
对于功能列表2中的f:
f(字)
调用函数()
只需使用以键为长度的max和min函数即可
y = []
for names in range(4):
name = raw_input('Enter:')
y += name,
s = max(y, key=len)
r = min(y, key=len)
尝试以下简单的解决方案:
def find_longest_and_shortest_word(list_of_words):
longest_word = list_of_words[0]
shortest_word = list_of_words[0]
for word in list_of_words:
if len(longest_word) < len(word):
longest_word = word
if len(shortest_word) > len(word):
shortest_word = word
print(f'The longest word is: {longest_word}')
print(f'The shortest word is: {shortest_word}')
return longest_word, shortest_word
def查找最长和最短单词(单词列表):
最长单词=单词列表[0]
最短单词=单词列表[0]
对于单词列表中的单词:
如果len(最长单词)len(单词):
最短的单词
打印(f'最长单词为:{longest_word}')
打印(f'最短单词为:{shortest_word}')
返回最长单词,最短单词
您可以使用sorted()
函数对列表进行排序,该函数允许您根据列表中字符串的长度对列表进行排序。为此,您需要添加key=len
,以便该函数将按长度排序,而不是按字母顺序排序。您还需要赋予函数reverse=true
因此,访问最长的字符串将更容易(它将位于列表的[0]中)
def最长(我的列表):
my_list=排序(my_list,key=len,reverse=True)
返回我的_列表[0]
列表1=['aaa','bbbb','CCCC','d']
打印(最长(列表1))
def longestWord(单词):
最长=“”
对于范围内的num(len(words)):
如果是len(最长的),我怎么才能找到最小的单词呢?我的输出总是给我“e”作为最小的单词。@Shayd3您在该函数中也有一个bug。用smallestLen=len(word)替换if块中的第一行。嘿,哈里什!在回答下一个问题之前,请仔细阅读!祝您在SO逗留愉快:)
y = []
for names in range(4):
name = raw_input('Enter:')
y += name,
s = max(y, key=len)
r = min(y, key=len)
def find_longest_and_shortest_word(list_of_words):
longest_word = list_of_words[0]
shortest_word = list_of_words[0]
for word in list_of_words:
if len(longest_word) < len(word):
longest_word = word
if len(shortest_word) > len(word):
shortest_word = word
print(f'The longest word is: {longest_word}')
print(f'The shortest word is: {shortest_word}')
return longest_word, shortest_word
def longestWord(words):
longest=''
for num in range(len(words)):
if len(longest)<len(words[num]):
longest=words[num]
print ('longest word is {}'.format(longest))
words = ["alpha","omega","up","down","over","under","purple","red","blue","green"]
#GET LONGEST WORD
max(words, key=len)
>>> 'purple'
#GET SHORTEST WORD
min(words, key=len)
>>> 'up'