简体   繁体   English

如果是元音则如何删除第一个字母,如果没有元音则如何不返回元音

[英]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.

相关问题 将元音替换为元音+字母+元音 Python - Replace vowels with vowel+letter+vowel Python 检查单词的第一个字母是否是元音 - checking if the first letter of a word is a vowel 如何在一个句子中的每个元音后检测并打印第一个字母? - How to detect and print first letter after each vowel in a sentence? Python 字符串迭代:如何先打印非元音字符,然后再打印元音(通过 For 循环) - Python string iteration: how to print any non-vowel chars first, then vowels last (via For loop) 在字符串中打印元音和元音计数 - Printing the vowels and the vowel count in a string Python代码-使用元音返回字符串中的第一个单词 - Python code - return first word in a string with vowel 如何在一个单词文件中找到一个没有字母 s 并且只包含一个元音的 7 个字母的单词? - How to find a 7 letter word without the letter s and contains only one vowel in a word file? 如果 String s 中没有字符是 Python 中的元音,则返回 True - Return True if no character in String s is a vowel in Python 验证列表中字符串的第一个字母是否为元音时出错,解决方案? - Error validating whether first letter of string in list is a vowel, solutions? 如何将元音字母后面的字母附加到列表中? - How to append a letter that comes after vowel letter to a list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM