使用Python中的循环计数字符串中的元音的数量

问题描述 投票:-1回答:9

Python代码:

sentence = input('Enter a string:')
vowel = 'A,a,E,e,I,i,O,o,U,u'
Count = 0
for vowel in sentence:
   Count += 1
print('There are {} vowels in the string: \'{}\''.format(Count,sentence))

我想写提示用户输入一个字符串的程序。然后该程序返回字符串中的元音的数量。但是,代码只是返回的字母数,而不只是回馈元音方面。

python
9个回答
3
投票

您已经定义了元音字母的字符串,而是重新定义在循环变量元音。

我建议界定元音的set,然后根据一个if检查递增的计数器。

vowels = set('aeiou')  

counter = 0
for c in sentence.lower():
    if c in vowels:
        counter += 1

在这里,if c in vowels将返回True如果c是元音(即c属于存储在vowels集合元音)。


你可以改善与collections.Counter对象此解决方案:

from collections import Counter

c = Counter(sentence.lower())
counter = sum(c[v] for v in set('aeiou'))

或者,你可以使用一个list comprehension并计算它的长度。


2
投票

我认为最好的方法是使用一个简单的正则表达式。匹配一个元音的正则表达式是[aeiou](或[aeiouy]如果你想匹配“Y”也是如此)。

您可以使用re.findall找到一个元音的所有出现在一个句子中,使用re.IGNORECASE标志忽略大小写。

>>> import re
>>> sentence = "my name is Jessica"
>>> len(re.findall(r'[aeiou]', sentence, flags=re.IGNORECASE))
6

该解决方案的好处是,你可以轻松地扩展它来添加强调字符或其他的Unicode字符:

>>> import re
>>> sentence = "Dès Noël où un zéphyr haï me vêt de glaçons würmiens je dîne d’exquis rôtis de bœuf au kir à l’aÿ d’âge mûr & cætera !"
>>> len(re.findall(r'[aeiouyàâèéêëiïîöôüûùæœÿ]', sentence, flags=re.IGNORECASE))
41

0
投票

你是不是在任何地方检查,如果在句子中的字母是元音字母。语法for x in y:定义了其中对于每个循环迭代中,变量x被设置为可迭代的下一元素的迭代的对象(字符串,列表等)的环。

你行for vowel in sentence:被简单地定义一个循环,其中vowel被分配到输入句子的每个字母。您以前的vowel = ...的声明没有影响这里。

一个体面的1班轮达到预期的结果将是:

sentence = input('Enter a string:')
vowels = 'aeiou'
count = len([c for c in sentence.lower() if c in vowels])

0
投票

使用Python 3:

sentence = input('Enter a string:')
vowels = 'A,a,E,e,I,i,O,o,U,u'
Count = 0
for each_char in sentence:
    if each_char in vowels:
        Count += 1 
print('There are {} vowels in the string: \'{}\''.format(Count,sentence))

0
投票

这是使用集合的计数器我的代码

import collections
vowel = ['a', 'e', 'i', 'o', 'u']
count = 0

sentence = input('Enter a string:').lower()
counter = collections.Counter(sentence)
for word in counter:
    if word in vowel:
        count += counter[word]

print('There are {} vowels in the string: \'{}\''.format(count,sentence))

0
投票
sentence = input('Enter a string:')
Count = 0
for eachLetter in sentence:
    if eachLetter.lower() in ['a','e','i','o','u']:
        Count += 1
print('There are {} vowels in the string: \'{}\''.format(Count,sentence))

0
投票

我们只需要小写元音的名单,因为我们把句子变成小写只有.lower()

然后我们遍历每个元音和计算多少每个元音的句子中找到具有.Count之间的()。然后我们汇总的总数,直到我们结束了元音字母的总数。

sentence = 'This is a sentence'
vowels = ['a','e','i','o','u']
Count = 0
for vowel in vowels:
    Count = Count + sentence.lower().count(vowel)

0
投票

没有必要让这个困难,甚至与for环约束:

VOWELS = 'aeiou'

sentence = input('Enter a string: ')

count = 0

for letter in sentence.lower():
    count += letter in VOWELS

print('There are {} vowels in the string: \'{}\''.format(count, sentence))

这样做的心脏是布尔也认为我们可以总结数字。您可以VOWELS一个set('aeiou'):如果您要处理大量文字,需要附加效率。我们可以更换count初始化并具有生成表达for循环:

count = sum(1 for c in sentence.lower() if c in VOWELS)

这可能不是一个列表理解,因为它不会产生暴殄天物列表,并避免处理信件的子集的第二次更好。


0
投票

那是因为你不验证,如果字母是元音。当你把“句子元音”的for循环你刚才覆盖的是元音和元音成为每次循环迭代在句子中的每个字母。

sentence = input('Enter a string:')
vowel = 'A,a,E,e,I,i,O,o,U,u'
Count = 0
for letter in sentence:
  if letter in vowel.split(','):
    Count += 1
print('There are {} vowels in the string: \'{}\''.format(Count,sentence))
© www.soinside.com 2019 - 2024. All rights reserved.