[英]how to remove first letter if it's vowel and return no vowels if there is no vowel
Write the function startWithVowel(word) that takes in a word as argument and returns a substring that starts with the first vowel found in the word. 编写函数startWithVowel(word),它接受一个单词作为参数,并返回一个以单词中找到的第一个元音开头的子字符串。 The function returns 'No vowel' if the word does not contain vowel.
如果单词不包含元音,则该函数返回'No vowel'。
Examples 例子
>>> startWithVowel('apple')
'apple'
>>> startWithVowel('google')
'oogle'
>>> startWithVowel('xyz')
'No vowel'
My Answer 我的答案
def startWithVowel(word):
if word[0] in 'aeiou':
return word
elif word[0] != 'aeiou' and word[1:] in 'aeiou':
return word[1::]
elif word not in 'aeiou':
return "No vowel"
MyQuestion I know how to remove first vowel letter if word contains any vowel, but i am stuck with this code.. using this logic it should return 'no vowel' for xyz. MyQuestion我知道如果单词包含任何元音,如何删除第一个元音字母,但我坚持使用这个代码..使用这个逻辑它应该为xyz返回'no vowel'。 but it's returning 'yz' and i know where i am wrong, it's a logical problem in line 4. but i don't know how to solve this problem.
但它正在返回'yz'并且我知道我错在哪里,这是第4行的逻辑问题。但我不知道如何解决这个问题。
Here is a rather straightforward way to do it: 这是一种相当简单的方法:
def startWithVowel(word):
for i, c in enumerate(word):
if c.lower() in 'aeiou':
return word[i:]
return 'No vowel'
>>> for word in 'apple', 'google', 'xyz', 'BOGGLE':
... print startWithVowel(word)
apple
oogle
No vowel
OGGLE
How about this - 这个怎么样 -
>>> def startWithVowel(word):
... while len(word) > 0 and word[0] not in 'aeiou':
... word = word[1:]
... if len(word) == 0:
... return 'No vowel'
... return word
...
>>> startWithVowel('apple')
'apple'
>>> startWithVowel('google')
'oogle'
>>> startWithVowel('xyz')
'No vowel'
>>> startWithVowel('flight')
'ight'
Use word[0].lower()
in the above loop, if you want to check case-insensitive. 如果要检查不区分大小写,请在上面的循环中使用
word[0].lower()
。
The problem with your logic in line 4 is that you are using !=
instead of not in
. 第4行逻辑的问题在于你正在使用
!=
而not in
。 What you are doing is comparing word[0] which is a character and comparing it to the string 'aeiou'
. 你正在做的是比较word [0]这是一个字符,并将它与字符串
'aeiou'
进行比较。 Naturally a single character will never be equal to a string. 当然,单个字符永远不会等于字符串。 On top of that,
word[1:] in 'aeiou'
and word in 'aeiou'
don't work. 最重要的是,
word[1:] in 'aeiou'
word in 'aeiou'
不起作用。 They are comparing strings, not iterating over each letter of the word and comparing the characters. 他们正在比较字符串,而不是迭代单词的每个字母并比较字符。 You can fix this by doing the following
您可以通过执行以下操作来解决此问题
def startWithVowel(word):
if word[0] in 'aeiou':
return word
else:
for i in range(1, len(word)):
if word[i] in 'aeiou': return word[i:]
return "No vowel"
This says if the first letter is a vowel return the word, else, iterate over each letter in the word and look for a vowel. 这表示如果第一个字母是元音,则返回单词,否则,迭代单词中的每个字母并查找元音。 If a vowel is found then return the word from that index and if not then return 'No vowel'
如果找到元音然后从该索引返回单词,如果没有,则返回'No vowel'
You may use re.search
. 您可以使用
re.search
。
import re
def findstring(s):
m = re.search(r'(?i)[aeiou].*', s)
if m:
return m.group()
else:
return "No vowel"
def startWithVowel(word):
dataLen=len(word)
newStr=''
if word[0] in 'aieou':
return word
for xx in range(1,dataLen):
count=0
if word[0] not in 'aieou' and word[xx] in "aieou" or word[xx] not in 'aieou':
newStr=newStr+word[xx]
if(word[xx] in 'aieou'):
count=count+1
if count>0:
return newStr
else:
return 'No vowel'
#return newStr
for xx in word:
if xx not in 'aieou':
return 'No vowel'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.