[英]Counting the number of vowels in a string using for loops in Python
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))
我正在尝试编写一个程序,提示用户输入字符串。 然后,程序返回字符串中的元音数量。 但是,该代码仅返回字母数,而不考虑仅返回元音。
您已经定义了一个元音字符串,但是正在循环中重新定义变量元音。
我建议定义一set
元音,然后基于if
检查增加您的计数器。
vowels = set('aeiou')
counter = 0
for c in sentence.lower():
if c in vowels:
counter += 1
在这里,如果c
是元音(即c
属于存储在vowels
中的vowels
集合),则元音中的if c in vowels
将返回True
。
您可以使用collections.Counter
对象改进此解决方案:
from collections import Counter
c = Counter(sentence.lower())
counter = sum(c[v] for v in set('aeiou'))
或者,您可以使用list comprehension
并计算其长度。
我认为最好的方法是使用简单的RegEx。 匹配元音的RegEx为[aeiou]
(如果您也想匹配“ y”,则为[aeiouy]
)。
您可以使用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
您不会在任何地方检查句子中的字母是否是元音。 for x in y:
的语法for x in y:
在一个可迭代对象(字符串,列表等)上定义一个循环,其中对于每次循环迭代,变量x
均设置为该可迭代对象的下一个元素。
您for vowel in sentence:
行for vowel in sentence:
仅定义了一个循环,其中将vowel
分配给输入句子的每个字母。 您先前的vowel = ...
声明vowel = ...
在这里无效。
达到预期效果的不错的1缸套将是:
sentence = input('Enter a string:')
vowels = 'aeiou'
count = len([c for c in sentence.lower() if c in vowels])
发生这种情况是因为您不验证字母是否是元音。 当您在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))
使用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))
这是我使用集合计数器的代码
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))
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))
我们只需要列表中的小写元音,因为仅使用.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)
即使有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')
为set('aeiou')
。 我们可以用生成器表达式替换count
初始化和for
循环:
count = sum(1 for c in sentence.lower() if c in VOWELS)
这可能比列表理解更好,因为它不会创建一次性列表,并且避免了第二次处理字母的子集。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.