【问题标题】:how to remove first letter if it's vowel and return no vowels if there is no vowel如果是元音,如何删除第一个字母,如果没有元音,如何不返回元音
【发布时间】:2015-07-27 11:22:00
【问题描述】:

编写函数 startWithVowel(word),它接受一个单词作为参数,并返回一个从单词中找到的第一个元音开始的子字符串。如果单词不包含元音,则该函数返回“No vowel”。

例子

>>> startWithVowel('apple')
'apple'
>>> startWithVowel('google')
'oogle'
>>> startWithVowel('xyz')
'No vowel'

我的答案

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"

我的问题 如果单词包含任何元音,我知道如何删除第一个元音字母,但我坚持使用此代码..使用此逻辑它应该为 xyz 返回“无元音”。 但它返回'yz',我知道我错了,这是第4行的逻辑问题。但我不知道如何解决这个问题。

【问题讨论】:

    标签: python


    【解决方案1】:

    这是一个相当简单的方法:

    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
    

    【讨论】:

      【解决方案2】:

      这个怎么样-

      >>> 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'
      

      如果要检查不区分大小写,请在上述循环中使用word[0].lower()

      【讨论】:

        【解决方案3】:

        第 4 行中的逻辑问题在于您使用的是!= 而不是not in。您正在做的是比较作为字符的 word[0] 并将其与字符串 'aeiou' 进行比较。自然,单个字符永远不会等于字符串。最重要的是,word[1:] in 'aeiou'word in 'aeiou' 不起作用。他们正在比较字符串,而不是遍历单词的每个字母并比较字符。您可以通过执行以下操作来解决此问题

        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"
        

        这表示如果第一个字母是元音,则返回单词,否则,遍历单词中的每个字母并查找元音。如果找到元音,则返回该索引中的单词,如果没有,则返回 'No vowel'

        【讨论】:

          【解决方案4】:

          您可以使用re.search

          import re
          def findstring(s):
              m = re.search(r'(?i)[aeiou].*', s)
              if m:
                  return m.group()
              else:
                  return "No vowel"
          

          【讨论】:

          • NameError: global name 're' is not defined :) 此外,该函数应该返回字符串,而不是打印它。
          • import re 解决NameError: global name 're' is not defined
          • @SparkandShine:我知道,你知道,Avinash 知道。但是OP可能不会。确实应该在答案中提及。
          • 为什么你们讨厌正则表达式而喜欢 for 循环的答案?是关于性能?
          【解决方案5】:
          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'            
          

          【讨论】:

          • 你不应该只是把代码放在答案中,更好地描述你做了什么以及为什么这是一个解决方案。
          猜你喜欢
          • 2023-03-07
          • 1970-01-01
          • 2017-04-01
          • 1970-01-01
          • 2016-01-13
          • 2023-02-03
          • 2018-04-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多