我试图弄清楚如何在句子中获得辅音的表示。我刚刚使用的代码似乎没有完成这项工作:
vowels = ("aeiou")
count = 0
for x in text:
if not x in vowels:
count += 1
在示例“hello world”作为输入中,我收到 8 个辅音。提前谢谢了。
我试图弄清楚如何在句子中获得辅音的表示。我刚刚使用的代码似乎没有完成这项工作:
vowels = ("aeiou")
count = 0
for x in text:
if not x in vowels:
count += 1
在示例“hello world”作为输入中,我收到 8 个辅音。提前谢谢了。
你最好直接检查辅音,而不是“不是元音,不是空格,不是标点符号和......”
consonants = "bcdfghjklmnpqrstvwxyz"
count = 0
for x in text:
if x in consonants:
count += 1
您正在计算所有字符,包括空格。此外,您还需要包含标点符号、空格和任何其他非辅音字符。
更直接
consonants = set("bcdfghjklmnpqrstvwxyz")
count = sum(1 for c in text if c in consonants)
为辅音使用一组应该会使查找更快一点
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
如果速度确实起作用,编译的正则表达式似乎是获得计数的最快方法。
计时结果
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)
不要忘记考虑空格。
根据您的代码,8 看起来像是正确答案。
空格字符不在您的元音列表中。
也许您可以使用以下代码:
consonants = list("bcdfghjklmnpqrstvwxyz")
word=" hello world "
number_of_consonants = sum(word.count(c) for c in consonants)
使用非元音条件、字典理解和Counter
.
import collections as ct
text = "Hello world!"
vowels = ("aeiou")
letters = ct.Counter(text.lower())
对于前面显示的类似技术,不需要一长串辅音。isalpha
您仍然可以使用and搜索不是元音的字母 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
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
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)