【发布时间】:2019-10-16 14:04:46
【问题描述】:
我正在开发一个函数,该函数可以计算恰好有五个字母的列表中单词的数量(包括像 can't 这样的缩略词)。
我在互联网上搜索了类似的问题,但空手而归。
def word_count(wlist):
""" This function counts the number of words (including contractions like couldn't) in a list w/ exactly 5
letters."""
w = 0
for word in x:
w += 1 if len(word) == 5 else 0
return w
x = ["adsfe", "as", "jkiejjl", "jsengd'e", "jjies"]
print(word_count(x))
我希望这个函数可以计算列表中的单词数量(包括像 can't 这样的缩写词)正好有五个字母。感谢您提供任何反馈。
【问题讨论】:
-
我的原始帖子已被编辑以反映有关重复主题的问题。
标签: python string list function sum