如何保留元音字母中所有单词的开头,但删除字符串的其余部分
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)
输出
witht sm vwls
或者,让每一个单词的第一个字符,你可以使用一个标记值,每次一个非字母字符这样的标志作为标点符号或存在空格,然后保留下一个字符而不是其他字符。
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)
输出
w s v
这是执行时应该返回的内容。我需要定义函数,而不是创建一个运行该函数的程序。 shortenPlus('空载燕子的空速是多少')=='用户输入的是一个未登录的swll的词汇空间,“没有一些元音”只是说明函数将要做什么。 – Taylor
@泰勒解决了哥们! http://*.com/a/42142878/6840615 –
def shortenPlus(s):
counter = 0 # accepted character count
add_the_vowel = True # check if vowel came for the first time for the word
temp = " " # temp string to store the output
for letter in s:
if letter == " ":
add_the_vowel= True
if add_the_vowel == True and letter in "AEIOUaeiou":
temp += s[counter] # first vowel of the word
if letter in "AEIOUaeiou":
add_the_vowel = False # restrict second vowel appeared
else:
temp += s[counter]
counter += 1
print(temp)
s = "without some vowels frienis"
shortenPlus(s)
如何保持元音在字符串中的所有单词开始,但在字符串的其余部分去除
输出:
witht som vowls frins
希望这可以解决您的问题! http://*.com/a/42142878/6840615 –