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

该博客介绍了如何在Python中编写函数来查找单词列表中的最长和最短单词。作者提供了一个包含多个单词的列表,并定义了`bigWords`和`smallWords`两个函数,分别用于找出最长和最短的单词。然而,当调用这些函数时,它们没有正确地显示列表中的单词,而是返回空结果。作者寻求帮助以解决这个问题。
摘要由CSDN通过智能技术生成

我有一个单词列表: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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值