我想计算一个文本中的元音,我可以这样做。但是我也想要统计一个辅音的元音数量和一个元音的元音数量。有人可以帮助我吗?计数元音其次是辅音
这是我迄今为止,但它说,无效的语法,我在元音中使用i + 1的语法。
s = "text"
vowels = set("aeuio")
consonants = set("qwrtypsdfghjklzxcvbnm")
vowelcount=0
vowelvowelcount=0
consonantcount=0
consvowelcount=0
consconscount=0
vowelconscount=0
for i in s:
if i in vowels:
vowelcount += 1
if i in consonants:
consonantcount +=1
for i in s:
if (i in vowels,and i+1 in vowels):
vowelvowelcount +=1
print ("number of vowels:", vowelcount)
print ("number of consonants:", consonantcount)
print ("number of vowels followed by vowels:", vowelvowelcount)
你会想要使用字符串切片并计算你的方式通过字符串。就像元音中的s [2:3]和元音中的s [3:4]一样。 – mba12
@ mba12:但是[3:4]'相当于'[3]'... –
@WillemVanOnsem你是对的,你的语法也很干净。我在看到你的答案之前写了我的评论。 – mba12