Python:在列表中查找最长/最短单词并在函数中调用它们

5

我有一个单词列表:

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: .

不确定原因,需要帮助。

8个回答

19

如果你愿意,有更简单的方法来解决这个问题:

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.

4
只需使用max和min函数,将键设置为长度即可。
y = []
for names in range(4):
   name = raw_input('Enter:')
   y += name,
s = max(y, key=len)
r = min(y, key=len)

1

你离成功很近了 - 但我认为问题在于callFunctions()函数。你正在将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()

0

你可以使用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))


0
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'

0

首先,在函数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()

我该如何找到最小的单词?我的输出一直给出“e”作为最小的单词。 - Shayd3
@Shayd3,你的函数也有一个错误。请将if块中的第一行替换为smallestLen=len(word)。 - wasserfeder

0
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))

尝试调用:longestWord(['longerthanlongest','long','longer','longest'])
同样的方法也可以用来找到最小值。

嗨,哈里什!在回答下一个问题之前,请先阅读有关撰写答案的内容!祝您在SO上玩得愉快 :) - Diggy.

0

尝试这个简单的解决方案:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接