如何在 Python 中检测元音和辅音?
在英语字母表中,元音是字母 a、e、i、o 和 u。包含元音的单词示例包括“apple”(苹果)、“elephant”(大象)、“igloo”(冰屋)、“octopus”(章鱼)和“umbrella”(雨伞)。
辅音是英语字母表中的所有其他字母,包括 b、c、d、f、g、h、j、k、l、m、n、p、q、r、s、t、v、w、x、y 和 z。包含辅音的单词示例包括“ball”(球)、“cat”(猫)、“dog”(狗)、“fish”(鱼)、“green”(绿色)和“hat”(帽子)。
以下是一些带有分步说明的代码示例,用于在 Python 中检测元音和辅音
使用 for 循环和条件语句
示例
定义要检查元音和辅音的字符串。
将元音和辅音的计数器初始化为 0。
遍历字符串中的每个字符。
检查字符的小写版本是否在字符串“aeiou”中。如果是,则增加元音计数器。
如果字符是字母但不是元音,则增加辅音计数器。
打印计数器的结果。
string = "lorem ipsum"
vowels = 0
consonants = 0
for char in string:
if char.lower() in "aeiou":
vowels += 1
elif char.isalpha():
consonants += 1
print("Number of vowels:", vowels)
print("Number of consonants:", consonants)
输出
Number of vowels: 4 Number of consonants: 6
使用列表推导式和集合
示例
定义要检查元音和辅音的字符串。
使用列表推导式创建一个包含字符串中所有小写元音字符的列表。
使用内置的 len() 函数获取列表中的项目数,即元音的个数。
使用另一个列表推导式创建一个包含字符串中所有不是小写元音的字母字符的列表。
使用内置的 len() 函数获取列表中的项目数,即辅音的个数。
打印计数器的结果。
string = "lorem ipsum"
vowels = len([char for char in string if char.lower() in {'a', 'e', 'i', 'o', 'u'}])
consonants = len([char for char in string if char.isalpha() and char.lower() not in {'a', 'e', 'i', 'o', 'u'}])
print("Number of vowels:", vowels)
print("Number of consonants:", consonants)
输出
Number of vowels: 4 Number of consonants: 6
使用正则表达式
示例
导入内置的 re 模块,该模块提供对正则表达式的支持。
定义要检查元音和辅音的字符串。
使用 re.findall() 函数查找正则表达式模式“[aeiou]”的所有匹配项,该模式匹配任何小写元音,不区分大小写。
使用内置的 len() 函数获取匹配项的数量,即元音的个数。
再次使用 re.findall(),并使用正则表达式模式“[b-df-hj-np-tv-z]”,该模式匹配任何小写辅音,不区分大小写。
再次使用 len() 获取匹配项的数量,即辅音的个数。
打印计数器的结果。
import re
string = "lorem ipsum"
vowels = len(re.findall("[aeiou]", string, re.IGNORECASE))
consonants = len(re.findall("[b-df-hj-np-tv-z]", string, re.IGNORECASE))
print("Number of vowels:", vowels)
print("Number of consonants:", consonants)
输出
Number of vowels: 4 Number of consonants: 6
使用集合和交集
示例
定义要检查元音和辅音的字符串。
使用 set() 函数创建一个包含字符串中所有字符的集合,并将其转换为小写。
使用 set.intersection() 方法查找此集合和小写元音集合之间的交集,并使用 len() 函数获取结果集合中的元素数量。
重复步骤 3,但使用小写辅音集合。
打印计数器的结果。这里重复的字符只计算一次。
string = "lorem ipsum"
vowels = len(set(string.lower()).intersection({'a', 'e', 'i', 'o', 'u'}))
consonants = len(set(string.lower()).intersection(set('bcdfghjklmnpqrstvwxyz')))
print("Number of vowels:", vowels)
print("Number of consonants:", consonants)
输出
Number of vowels: 4 Number of consonants: 5
使用字典和字符串格式化
示例
定义要检查元音和辅音的字符串。
创建一个字典,其中键为“元音”和“辅音”,并将它们的初始值设置为 0。
遍历字符串中的每个字符。
如果字符的小写版本在字符串“aeiou”中,则增加字典中“元音”的值。
如果字符是字母但不是元音,则增加字典中“辅音”的值。
使用字符串格式化打印字典中计数器的结果。
string = "lorem ipsum"
count = {'vowels': 0, 'consonants': 0}
for char in string:
if char.lower() in 'aeiou':
count['vowels'] += 1
elif char.isalpha():
count['consonants'] += 1
print("Number of vowels: {vowels}\nNumber of consonants: {consonants}".format(**count))
Output
Number of vowels: 4
Number of consonants: 6
使用 Counter 和 filter
示例
从 collections 模块导入 Counter 类。
定义要检查元音和辅音的字符串。
使用 filter() 函数创建一个仅包含小写元音字符的新迭代器,并使用 Counter() 构造函数计算每个元音出现的次数。
将每个元音的计数相加以获取元音的总数。
重复步骤 3-4,但过滤不是元音的字母字符,并使用 sum() 函数获取辅音的总计数。
打印计数器的结果。
from collections import Counter
string = "lorem ipsum"
count = Counter(filter(lambda c: c.lower() in 'aeiou', string))
vowels = count['a'] + count['e'] + count['i'] + count['o'] + count['u']
count = Counter(filter(lambda c: c.isalpha() and c.lower() not in 'aeiou', string))
consonants = sum(count.values())
print("Number of vowels:", vowels)
print("Number of consonants:", consonants)
输出
Number of vowels: 4 Number of consonants: 6
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP