Python 3.x 如何将元音保留在字符串中所有单词的开头,但在字符串的其余部分删除

Python 3.x 如何将元音保留在字符串中所有单词的开头,但在字符串的其余部分删除,python-3.x,Python 3.x,我已经把它从整个绳子上取下来了。但是我不知道如何将replace函数限制为字符串中每个单词的第一个字母以外的所有字符。不确定您要查找的确切内容,您能否澄清,或者给出一个简单的示例?示例中的所有单词都不是以元音开头的 但是在这里你可以去掉一个单词中的所有元音,除了第一个单词的第一个元音。硬编码,但给你一个想法: def shortenPlus(s) -> "s without some vowels": for char in s: if char in "AEIOU

我已经把它从整个绳子上取下来了。但是我不知道如何将replace函数限制为字符串中每个单词的第一个字母以外的所有字符。

不确定您要查找的确切内容,您能否澄清,或者给出一个简单的示例?示例中的所有单词都不是以元音开头的

但是在这里你可以去掉一个单词中的所有元音,除了第一个单词的第一个元音。硬编码,但给你一个想法:

def shortenPlus(s) -> "s without some vowels":
    for char in s:
        if char in "AEIOUaeiou":
            return(s.replace(char,""))
输出

s="without some vowels"
for char in s[2:]:
    if char in "AEIOUaeiou":
        s=s.replace(char,"")
print(s)
s="without some vowels"
sent=2
for char in s:
if sent>0:
    sent-=1
    print(char)
    continue
if not char.isalpha():
    sent=2
    continue
s=s.replace(char,"")
print(output)
或者,要获取每个单词的第一个字符,可以使用一个哨兵值,该值在每次出现标点符号或空格等非字母字符时进行标记,然后保留下一个字符,但不保留其他字符

witht sm vwls
输出

s="without some vowels"
for char in s[2:]:
    if char in "AEIOUaeiou":
        s=s.replace(char,"")
print(s)
s="without some vowels"
sent=2
for char in s:
if sent>0:
    sent-=1
    print(char)
    continue
if not char.isalpha():
    sent=2
    continue
s=s.replace(char,"")
print(output)
如何将元音保留在字符串中所有单词的开头,但在字符串的其余部分删除元音

输出:


这就是它在执行时应该返回的内容。我需要定义函数,而不是制作一个运行函数的程序。shortenPlus(‘空载燕子的空速是多少’)==‘当用户输入unldn swllw的arspd vlcty时,“没有一些元音”只是说明函数要做什么。希望这能解决您的问题!