Advertisement

python找到一行单词中最长的,Python:在列表中查找最长/最短的单词并在函数中调用它们...

阅读量:

我包含了一个单词列表:words = ["alpha", "omega", "up", "down", "over", "under", "purple", "red", "blue", "green"]
我的两个函数分别用于查找该列表中的最简短单词和最长单词。

def bigWords(list=[], *args):

largestWord=""

largestLen=0

for word in list:

if largestWord

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)

还没有任何评论哟~