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

时间:2014-10-01 01:30:13

标签: python function loops for-loop maps

我有一个单词列表:         词语= [ “下”, “阿尔法”, “Ω”, “上”, “下”, “上方”, “紫色”, “红色”, “蓝色”, “绿色”] 我有两个函数可以找到这个列表中最短和最长的单词:

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 个答案:

答案 0 :(得分:14)

如果您愿意,可以采用更简单的方法解决问题:

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.

答案 1 :(得分:2)

  

只需使用具有密钥的max和min函数作为长度

    y=[]
    for names in range(4):
       name=raw_input('Enter:')
       y+=name,
    s=max(y,key=len)
    r=min(y,key=len)

答案 2 :(得分: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()

答案 3 :(得分: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()

答案 4 :(得分: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

答案 5 :(得分: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))

答案 6 :(得分: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'])

可以做同样的事情来找到最可爱的人。

答案 7 :(得分: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'