如何制作一个函数来检查字符串中有多少个辅音字母或元音字母?
How to make a function checking how many consonants or vowels are in a string?
我正在学习佐治亚理工学院的 CS1301xII 课程,我一直被这个问题难住了。
我应该创建一个名为 count_letters 的函数。如果 find_consonants 为真,则计算辅音,如果为假,则计算元音。它应该只有 return 个元音和辅音,没有大写字母、空格、数字或标点符号。我得到的输出是 0 个辅音,然后是 1 个元音。我预计 14,然后 7。
def count_letters(string, find_consonants):
if find_consonants == True:
count = 0
for i in string:
if i == ("q" or "w" or"r" or "t" or "y" or "p" or "s" or "d" or "f" or "g" or "h" or "j" or "k" or "l" or "z" or "x" or "c" or "v" or "b" or "n" or "m"):
count += 1
return count
else:
count = 0
for i in string:
if i == ("a" or "e" or "i" or "o" or "u"):
count += 1
return count
(下一段是我自测,autograder改字符串)
a_string = "白色和金色搭配"
print(count_letters(a_string, True))
print(count_letters(a_string, False))
遗憾的是,您不能使用 if var == "foo" or "bar"
;你必须写 if var == "foo" or var == "bar"
。
对于您的功能,请尝试类似:
def count(string, count_consonants):
vowelcount = 0
consonantcount = 0
for letter in string:
if letter in "aeiou": #Detect if letter is in vowel list
vowelcount += 1
elif letter in "bcdfghjklmnpqrstvwxyz":
consonantcount += 1
if count_consonants:
return consonantcount
else:
return vowelcount
测试if find_consonants == True
is a bit overengineered. if find_consonants
suffices. You might also want to make use of list comprehensions,避免显式循环。
这应该可以工作,例如:
def count_letters(s, find_consonants):
if find_consonants:
return len([l for l in s if l in "bcdfghjklmnpqrstvwxyz"])
return len([l for l in s if l in "aeiou"])
@ObseleteAwareProduce 的解决方案是正确的,并且解决了检查项目是否与特定列表中的任何项目匹配的概念。但就查找元音和辅音而言,我们也可以使用 re 库
import re
regex = re.compile("a|e|i|o|u", flags=re.I)
vowels_count = len(regex.finditer(input_string))
python中的or
操作是惰性求值,如:
>>> ('a' or 'c')
'a'
>>> ('c' or 'b' or 'a')
'c'
所以i == ("a" or "e" or "i" or "o" or "u")
相当于i == 'a'
,不是你想要的结果
你可以这样改
选项 1
这太疯狂了……不过
count = 0
for i in string:
if (i == "a") or (i == "e") or (i == "i") or (i == "o") or (i == "u"):
count += 1
选项2
这个比较优雅。
count = 0
for i in string:
if i in 'aeiou':
count += 1
选项 3
这是Python
len([x for x in string if x in 'aeiou'])
我正在学习佐治亚理工学院的 CS1301xII 课程,我一直被这个问题难住了。 我应该创建一个名为 count_letters 的函数。如果 find_consonants 为真,则计算辅音,如果为假,则计算元音。它应该只有 return 个元音和辅音,没有大写字母、空格、数字或标点符号。我得到的输出是 0 个辅音,然后是 1 个元音。我预计 14,然后 7。
def count_letters(string, find_consonants):
if find_consonants == True:
count = 0
for i in string:
if i == ("q" or "w" or"r" or "t" or "y" or "p" or "s" or "d" or "f" or "g" or "h" or "j" or "k" or "l" or "z" or "x" or "c" or "v" or "b" or "n" or "m"):
count += 1
return count
else:
count = 0
for i in string:
if i == ("a" or "e" or "i" or "o" or "u"):
count += 1
return count
(下一段是我自测,autograder改字符串) a_string = "白色和金色搭配"
print(count_letters(a_string, True))
print(count_letters(a_string, False))
遗憾的是,您不能使用 if var == "foo" or "bar"
;你必须写 if var == "foo" or var == "bar"
。
对于您的功能,请尝试类似:
def count(string, count_consonants):
vowelcount = 0
consonantcount = 0
for letter in string:
if letter in "aeiou": #Detect if letter is in vowel list
vowelcount += 1
elif letter in "bcdfghjklmnpqrstvwxyz":
consonantcount += 1
if count_consonants:
return consonantcount
else:
return vowelcount
测试if find_consonants == True
is a bit overengineered. if find_consonants
suffices. You might also want to make use of list comprehensions,避免显式循环。
这应该可以工作,例如:
def count_letters(s, find_consonants):
if find_consonants:
return len([l for l in s if l in "bcdfghjklmnpqrstvwxyz"])
return len([l for l in s if l in "aeiou"])
@ObseleteAwareProduce 的解决方案是正确的,并且解决了检查项目是否与特定列表中的任何项目匹配的概念。但就查找元音和辅音而言,我们也可以使用 re 库
import re
regex = re.compile("a|e|i|o|u", flags=re.I)
vowels_count = len(regex.finditer(input_string))
python中的or
操作是惰性求值,如:
>>> ('a' or 'c')
'a'
>>> ('c' or 'b' or 'a')
'c'
所以i == ("a" or "e" or "i" or "o" or "u")
相当于i == 'a'
,不是你想要的结果
你可以这样改
选项 1
这太疯狂了……不过
count = 0
for i in string:
if (i == "a") or (i == "e") or (i == "i") or (i == "o") or (i == "u"):
count += 1
选项2
这个比较优雅。
count = 0
for i in string:
if i in 'aeiou':
count += 1
选项 3
这是Python
len([x for x in string if x in 'aeiou'])