我是初学者,我有一个需要帮助的问题.它是功课,所以任何提示都受到赞赏.我看过一些类似的话题,但答案超出了我所知道的怎么做......
我需要计算文本文件中音节的数量,作为更大程序的一部分.除了音节,我有我需要的一切.我尝试了几种不同的方法,但它并不总能捕捉特殊情况.我应该'计算相邻元音的组,不包括单词末尾的'e'.我明白这意味着什么,但我无法在我的计划中做到这一点.这是我的:::
def syllables(word):
syl = 0
vowels = 'aeiouy'
starts = ['ou','ei','ae','ea','eu','oi']
endings = ['es','ed','e']
word = word.lower().strip(".:;?!")
for vowel in vowels:
syl +=word.count(vowel)
for ending in endings:
if word.endswith(ending):
syl -=1
for start in starts:
if word.startswith(start):
syl -=1
if word.endswith('le'):
syl +=1
if syl == 0:
syl+=1
return syl
Run Code Online (Sandbox Code Playgroud)
编辑:新代码
def syllables(word):
count = 0
vowels = 'aeiouy'
word = word.lower().strip(".:;?!")
if word[0] in vowels:
count +=1
for index in range(1,len(word)):
if word[index] in vowels and word[index-1] …Run Code Online (Sandbox Code Playgroud) 如果用户名不是某个用户,我正在制作一个带有不发布选项的机器人。
Reddit 用户名在这两种情况下都可以包含字母和数字。
哪个正则表达式可用于识别这样的用户名?格式是/u/USERNAME用户名可以包含大小写和数字的字母,例如ExaMp13.
我试过了 /u/[A-Z][a-z][0-9]