2014-10-01 34 views
1

我有一個單詞列表: words = [「alpha」,「omega」,「up」,「down」,「over」 「下」,「紫色」,「紅」,「藍」,「綠」] 我有,都應該找到最短和最長的單詞,在這個列表中有兩個功能:Python:查找列表中的最長/最短的單詞並在函數中調用它們

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

不知道爲什麼,任何幫助表示讚賞。

回答

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

我將如何去尋找最小的字?我的輸出不斷給我「e」作爲最小的單詞。 – Shayd3 2014-10-01 01:56:49

+0

@ Shayd3你也有該功能的錯誤。將if塊中的第一行替換爲smallestLen = len(word)。 – wasserfeder 2014-10-01 02:06:37

5

如果你喜歡,也有更簡單的方法來解決這個問題:

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

你是如此親密 - 但我認爲問題出在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() 
2

只需使用最大值和最小值的功能具有關鍵的長度

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