def shortenPlus(s) -> "s without some vowels":
for char in s:
if char in "AEIOUaeiou":
return(s.replace(char,""))
我已經將它從整個字符串中取出。但我無法弄清楚如何限制替換函數除了字符串中每個單詞的第一個字母。如何保留元音字母中所有單詞的開頭,但刪除字符串的其餘部分
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://stackoverflow.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://stackoverflow.com/a/42142878/6840615 –