Sha*_*yd3 4 python maps loops for-loop function
我有一个单词列表: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 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: .
Run Code Online (Sandbox Code Playgroud)
不知道为什么,任何帮助表示赞赏.
Joh*_*024 16
如果您愿意,可以采用更简单的方法来解决问题:
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],)
Run Code Online (Sandbox Code Playgroud)
这会产生:
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.
Run Code Online (Sandbox Code Playgroud)
小智 0
首先,您的 function 代码有错误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
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:
f(words)
callFunctions()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23475 次 |
| 最近记录: |