首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python - 编写代码,用于计算句子中包含“a”或“e”的单词数。将结果存储在变量num_a_or_e 中

Python - 编写代码,用于计算句子中包含“a”或“e”的单词数。将结果存储在变量num_a_or_e 中
EN

Stack Overflow用户
提问于 2019-04-20 22:04:54
回答 2查看 0关注 0票数 0

寻求帮助

代码语言:javascript
复制
    sentence = "python is a high level general purpose programming 
    language that can be applied to many different classes of problems."

    num_a_or_e = 0
    for i in sentence:
        if i ==['a'] or i == ['e']:
            num_a_or_e += 1

    print(num_a_or_e)
EN

回答 2

Stack Overflow用户

发布于 2019-04-21 06:58:21

你需要检查收容。你可以做到in。此外,您需要迭代单词。对于这个例子,.split()应该给你单词(它由白色空格夹板)。

代码语言:javascript
复制
sentence = '''python is a high level general purpose programming language that can be applied to many different classes of problems.'''

num_a_or_e = 0
for i in sentence.split():
    if ('a' in i) or ('e' in i):
        num_a_or_e += 1

print(num_a_or_e)
票数 0
EN

Stack Overflow用户

发布于 2019-04-21 07:59:45

试试这个代码

代码语言:javascript
复制
import re
sentence = " the following example"
list =re.findall("[ae]\w", sentence)
print(list)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100006648

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档