我正试图理解,如何在一个句子中表达辅音。 我刚刚使用的代码似乎没有完成这项工作:
vowels = ("aeiou")
count = 0
for x in text:
if not x in vowels:
count += 1
在示例“hello world”中作为输入,我收到8个辅音。 非常感谢提前。
答案 0 :(得分:7)
你最好直接检查辅音,而不是'不是元音而不是空格而不是标点符号......'
consonants = "bcdfghjklmnpqrstvwxyz"
count = 0
for x in text:
if x in consonants:
count += 1
答案 1 :(得分:3)
您正在计算包括空格在内的所有字符。此外,您还希望包含标点符号,空格和任何其他非辅音字符。
答案 2 :(得分:2)
更直接
consonants = set("bcdfghjklmnpqrstvwxyz")
count = sum(1 for c in text if c in consonants)
使用辅音组可以使查找更快一些
答案 3 :(得分:1)
import string
all_letters = string.ascii_letters
consonants = set(all_letters).difference(set(('a','e','i','o','u','A','E','I','O','U')))
my_sentence = 'Here is my Sentence'
sum_of_cons = sum(ele in consonants for ele in my_sentence)
结果
>>> sum_of_cons
10
答案 4 :(得分:1)
如果速度确实发挥作用,编译的正则表达式似乎是获得计数的最快方法。
时间安排
Found 8292 Consonants in 0.002978 seconds using compiled regex
Found 8292 Consonants in 0.009412 seconds using sets
Found 8292 Consonants in 0.024511 seconds by looking at each character
测试代码
import re
import time
import os
string_length = 100000
random_string = os.urandom(string_length)
con_re = re.compile("[bcdfghjklmnpqrstvwxyz]")
start = time.clock()
re_results = con_re.findall(random_string)
print "Found %d Consonants in %f seconds using compiled regex" % (len(re_results), time.clock() - start)
consonants = set("bcdfghjklmnpqrstvwxyz")
start = time.clock()
count = sum(1 for c in random_string if c in consonants)
print "Found %d Consonants in %f seconds using sets" % (count, time.clock() - start)
cnt = 0
consonants = "bcdfghjklmnpqrstvwxyz"
start = time.clock()
for x in range(string_length):
if random_string[x] in consonants:
cnt += 1
print "Found %d Consonants in %f seconds by looking at each character" % (cnt, time.clock() - start)
答案 5 :(得分:0)
不要忘记考虑空白。
8看起来像是基于您的代码的正确答案。
空格字符不在您的元音列表中。
答案 6 :(得分:0)
也许您可以使用此代码:
consonants = list("bcdfghjklmnpqrstvwxyz")
word=" hello world "
number_of_consonants = sum(word.count(c) for c in consonants)
答案 7 :(得分:0)
使用非元音条件,词典理解和 Counter
的替代解决方案。
import collections as ct
text = "Hello world!"
vowels = ("aeiou")
letters = ct.Counter(text.lower())
对于先前所示的类似技术,不需要具有长的辅音列表。您仍然可以使用isalpha
和not in vowels
搜索非元音的字母。 isalpha
消除了空格和标点符号:
# Option 1: Non-vowels
sum(1 for letter in text.lower() if letter.isalpha() and not letter in vowels)
# 7
对于更精细的细节,我们可以使用Counter
对象来计算文本中的每个独特字母和字典理解以保留所有辅音:
# Option 2: Dictionary comprehension
comp = {k:v for k, v in letters.items() if letter.isalpha() and not letter in vowels}
comp
# {'d': 1, 'h': 1, 'l': 3, 'r': 1, 'w': 1}
sum(comp.values())
# 7
此外,Counter
个对象具有有用的属性,例如.most_common()
和.elements()
。你当然也可以轻松地完成所有辅音。在这里,我们将仅为辅音制作一个特殊的Counter
:
# Option 3: Consonants counter
consonants = letters.copy()
for k in letters:
if k in vowels or not k.isalpha():
consonants.pop(k)
consonants
# Counter({'d': 1, 'h': 1, 'l': 3, 'r': 1, 'w': 1})
consonants.most_common(3)
#[('l', 3), ('d', 1), ('h', 1)]
list(consonants.elements())
# ['d', 'h', 'l', 'l', 'l', 'r', 'w']
sum(consonants.values())
# 7
答案 8 :(得分:0)
def count_consonants(sample_string):
spaces_removed = sample_string.replace(" ", "")
count = 0
vowels = ['a', 'e', 'i', 'o', 'u']
for char in spaces_removed.lower():
if char not in vowels:
count = count + 1
return count
答案 9 :(得分:-1)
def count_consonants(some_string):
some_string=some_string.lower();
consonants = list("bcdfghjklmnpqrstvwxyz")
number_of_consonants = sum(some_string.count(c) for c in consonants)
return number_of_consonants
test_str="Hercules was a hero"
result_str=count_consonants(test_str)
print(result_str)