我正在尝试删除字符串中元音的出现,除非它们是单词的开头。例如,像"The boy is about to win"
这样的输入应该输出Th by is abt t wn
。这是我到目前为止所拥有的。任何帮助将不胜感激!
def short(s):
vowels = ('a', 'e', 'i', 'o', 'u')
noVowel= s
toLower = s.lower()
for i in toLower.split():
if i[0] not in vowels:
noVowel = noVowel.replace(i, '')
return noVowel
答案 0 :(得分:6)
一种方法是使用正则表达式替换不在单词边界之前的元音。此外,如果您的代码应该使用各种类型的标点处理任意文本,您可能需要考虑一些更有趣的测试用例。
import re
s = "The boy is about to win (or draw). Give him a trophy to boost his self-esteem."
rgx = re.compile(r'\B[aeiou]', re.IGNORECASE)
print rgx.sub('', s) # Th by is abt t wn (or drw). Gv hm a trphy t bst hs slf-estm.
答案 1 :(得分:1)
尝试:
>>> s = "The boy is about to win"
>>> ''.join(c for i, c in enumerate(s) if not (c in 'aeiou' and i>1 and s[i-1].isalpha()))
'Th by is abt t wn'
以上关键部分如果是发电机:
c for i, c in enumerate(s) if not (c in 'aeiou' and i>1 and s[i-1].isalpha())
生成器的关键部分是条件:
if not (c in 'aeiou' and i>1 and s[i-1].isalpha())
这意味着s
中的所有字母都包括在内,除非它们不是(a)在s
开头的元音,因此在单词的开头,或者(b)之前的元音用非字母表示它们只是一个单词的开头。
for
循环def short(s):
new = ''
prior = ''
for c in s:
if not (c in 'aeiou' and prior.isalpha()):
new += c
prior = c
return new
答案 2 :(得分:0)
您可以在字符串的其余部分使用正则表达式(忽略第一个字符):
import re
s = 'The boy is about to win'
s = s[0] + re.sub(r'[aeiou]', '', s[1:])
print s # Th by s bt t wn
答案 3 :(得分:0)
使用正则表达式:
import re
re.sub("(?<!\b)[aeiouAEIOU]", '', s)
答案 4 :(得分:0)
通过re.sub。
>>> import re
>>> s = "The boy is about to win"
>>> re.sub(r'(?i)(?<=\S)[aeiou]', r'', s)
'Th by is abt t wn'
\S
匹配任何非空格字符。
答案 5 :(得分:0)
>>> re.sub('(?<=\w)[aeiou]','',"The boy is about to win")
'Th by is abt t wn'