我有一个单词列表:words = [“alpha”,“omega”,“up”,“down”,“over”,“under”,“purple”,“red”,“blue”,“green”]我有两个函数可以找到这个列表中最短和最长的单词:
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)
我嵌套了这些函数,所以我可以立即调用它们:
def callFunctions():
###Words###
words=["alpha","omega","up","down","over","under","purple","red","blue","green"]
wordLength=lenList(words)
print "The amount of words[] is %d" % wordLength
func_list2 = [bigWords, smallWords]
for f in func_list2:
map(f, words)
callFunctions()
这只是返回此而不输入列表中的单词:
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: .
不知道为什么,任何帮助表示赞赏。
如果您愿意,可以采用更简单的方法来解决问题:
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.
只需使用具有键作为长度的max和min函数
y=[]
for names in range(4):
name=raw_input('Enter:')
y+=name,
s=max(y,key=len)
r=min(y,key=len)
你是如此接近 - 但我认为问题是incallFunctions()
。您正在将func_list2
中的函数映射到单词数组中的每个字符串,而不是将该函数作为整体应用于数组。使用map是一个好主意,这是一个功能强大的功能,但你不需要在这里使用它。这是我用simple online interpreter测试的代码。试试吧。无论你正在学习什么/你正在制作的项目,祝你好运!
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()
首先,你在函数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
其次,要使用这两个函数,您需要将它们称为单词列表而不是每个单词:
def callFunctions():
###Words###
words=["alpha","omega","up","down","over","under","purple","red","blue","green"]
wordLength=lenList(words)
print "The amount of words[] is %d" % wordLength
func_list2 = [bigWords, smallWords]
for f in func_list2:
f(words)
callFunctions()
试试这个简单的方案:
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
您可以使用sorted()
函数对列表进行排序,该函数允许您按照字符串的长度对列表进行排序。因为您需要添加key=len
,因此函数将按长度排序,而不是按字母顺序排序。你还需要给函数reverse = true,这样就可以更容易地访问最长的字符串(它将在列表的[0]中)
def longest(my_list):
my_list = sorted(my_list, key=len, reverse=True)
return my_list[0]
list1 = ['aaa', 'bbbb', 'cccccc', 'd']
print(longest(list1))