python - 如何计算列表中包含特定字母的单词数量?
问题描述
我正在使用 Python 3。我正在寻找一种方法让 Python 遍历列表中的所有单词,并计算其中包含字母“e”的单词数量。我不想计算“e”出现的数量,只计算有一个或多个出现的单词的数量。
例如:
你好,你好,是否
我希望程序给出数字 2(因为列表中有两个项目包含“e”)
这是我没有工作的代码(我必须从单词列表中计数):
# defines the text to use
text = "Hello. My name is Elijah Beetle."
lettertocount = "e"
# specifies what punctuation to remove from text
punc = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
# removes the punctuation from text
for present in text:
if present in punc:
text = text.replace(present,"")
listofwords = text.split()
print(listofwords)
countofletter = 0
for counting in listofwords:
if counting in lettertocount:
countofletter += 1
print(countofletter)
解决方案
这是一个解决方案:
def e_words(words):
e_count = 0
for i in words:
if "e" in i:
e_count += 1
return e_count
print(e_words(["Hello", "Hi", "Whether"]))
该代码创建了一个名为e_words
that 的函数,该函数遍历列表并在单词中找到“e”时将其words
添加到变量中。e_count
推荐阅读
- android - 将详细信息加载到片段中导致操作未实现错误
- c# - 通过条件循环使用 LINQ 返回字符串列表
- flutter - 我可以在颤振小部件中使用宽度吗
- javascript - 请求方法在节点 js 中显示 403 错误
- elasticsearch - Jest 客户端 ElasticSearch 别名
- linux - 将 Docker 转换为 Singularity 时遇到问题:Singularity 中的“功能未实现”,但在 Docker 中工作正常
- r - 一列的一部分是否存在于另一列中?R
- ios - 是否可以使用 SCNAudioPlayer() 进行音频电平测量
- python - 如何在python中找到小说中所需的单词?
- javascript - ExtJS Classic:为窗口的 x/y/width/height 设置动画时,标题未在指定的最终位置结束