小编Sha*_*yd3的帖子

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<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)
Run Code Online (Sandbox Code Playgroud)

我嵌套了这些函数,所以我可以立即调用它们:

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()
Run Code Online (Sandbox Code Playgroud)

这只是返回此而不输入列表中的单词:

The longest …
Run Code Online (Sandbox Code Playgroud)

python maps loops for-loop function

4
推荐指数
2
解决办法
2万
查看次数

标签 统计

for-loop ×1

function ×1

loops ×1

maps ×1

python ×1