2017-02-09 58 views

回答

0

不确定你在找什么,你能澄清一下,也许举个简单的例子吗?你的例子中没有一个词是以元音开头的!

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

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 
+0

这是执行时应该返回的内容。我需要定义函数,而不是创建一个运行该函数的程序。 shortenPlus('空载燕子的空速是多少')=='用户输入的是一个未登录的swll的词汇空间,“没有一些元音”只是说明函数将要做什么。 – Taylor

+0

@泰勒解决了哥们! http://stackoverflow.com/a/42142878/6840615 –

0
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

相关问题