尝试创建一个单词计数函数,该函数将计算列表中给定长度的单词的数量。
my_list =['full', 'hello', 'light', 'too', 'wise']
如果我想在该列表中搜索 4 个字母的单词...我在想:
def word_count(list, length):
count = 0
if len(list) == 3: #I know this is wrong
#it's just an example of what I was thinking
count += 1
return count
任何帮助表示赞赏!
这是你想要的吗?
def countWordlen(lst):
count = 0
for i in lst: # loop over the items of the list
if len(i) == 3: # if the len the items (words) equals 3 increment count by 1
count = count + 1
return count
lst =['full', 'hello', 'light', 'too', 'wise']
countWordlen(lst)
OUT: 1
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句