我有一個單詞列表: 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: .
不知道爲什麼,任何幫助表示讚賞。
我將如何去尋找最小的字?我的輸出不斷給我「e」作爲最小的單詞。 – Shayd3 2014-10-01 01:56:49
@ Shayd3你也有該功能的錯誤。將if塊中的第一行替換爲smallestLen = len(word)。 – wasserfeder 2014-10-01 02:06:37