【问题标题】:In python, how do I find the vowels in a word?在python中,如何找到单词中的元音?
【发布时间】:2013-06-03 00:15:27
【问题描述】:

我正在尝试制作一个语言翻译器。在 python 中对我来说很简单的任务。或者我是这么想的。如果您不知道,向上语言是当您在每个元音之前加一个单词并说出它时。例如,安德鲁将是 Upandrupew。我试图找出如何在用户提交的单词中找到所有元音,并放在它们之前。有没有办法在所有元音之前切掉一个单词。如此出色会非常出色吗?谢谢。

【问题讨论】:

标签: python


【解决方案1】:

也许

VOWELS = 'aeiou'
def up_it(word):
    letters = []
    for letter in word:
        if letter.lower() in VOWELS:
            letters.append('Up')
        letters.append(letter)
    return ''.join(letters)

可以简化为

def up_it(word):
    return ''.join('up'+c if c.lower() in 'aeiou' else c for c in word)

【讨论】:

    【解决方案2】:

    您可以使用正则表达式来做到这一点:

    import re
    a = "Hello World."
    b = re.sub("(?i)([aeiou])", "up\\1", a)
    

    (?i) 使其不区分大小写。 \\1 指的是在([aeiou]) 中匹配的字符。

    【讨论】:

    • 添加一个 + 来捕获一组元音,即re.sub("(?i)([aeiou]+)", "up\\1", a)。用“美丽”之类的词来尝试你的解决方案,你就会明白为什么它不理想。
    • @jsdalton 我不熟悉“向上语言”,所以我遵循了 OP 问题中的规范字母。不过你的建议看起来会更好!
    【解决方案3】:
    ''.join(['up' + v if v.lower() in 'aeiou' else v for v in phrase])
    

    【讨论】:

      【解决方案4】:
      for vowel in [“a“,“e“,“i“,“o“,“u“]:
          Word = Word.replace(vowel,“up“+vowel)
      print(Word)
      

      【讨论】:

        【解决方案5】:
        import re
        sentence = "whatever"
        q = re.sub(r"([aieou])", r"up\1", sentence, flags=re.I)
        

        【讨论】:

          【解决方案6】:
          vowels = ['a', 'e', 'i', 'o', 'u']
          
          def upped_word(word):
              output = ''
              for character in word:
                  if character.lower() in vowels:
                      output += "up"
                  output += character
              return output
          

          【讨论】:

            【解决方案7】:

            这里是整个问题的一条线

            >>> "".join(('up' + x if x.upper() in 'AEIOU' else x for x in 'andrew'))
            'upandrupew'
            

            【讨论】:

              【解决方案8】:

              这是一种方法。

              wordList = list(string.lower())
              
              wordList2 = []
              
              for letter in wordList:
                  if letter in 'aeiou':
                      upLetter = "up" + letter
                      wordList2.append(upLetter)
                  else:
                      wordList2.append(letter)
              
              "".join(wordList2)
              

              创建一个字母列表(wordList),遍历这些字母并将其附加到第二个列表,该列表在末尾连接。

              返回:

              10: 'upandrupew'
              

              一行:

              "".join(list("up"+letter if letter in "aeiou" else letter for letter in list(string.lower())))
              

              【讨论】:

                【解决方案9】:

                我可能会使用 RegExp,但已经有很多答案在使用它。我的第二个选择是地图功能,女巫最好然后遍历每个字母。

                >>> vowels = 'aeiou'
                >>> text = 'this is a test'
                >>> ''.join(map(lambda x: 'up%s'%x if x in vowels else x, text))
                'thupis upis upa tupest'
                >>> 
                

                【讨论】:

                  【解决方案10】:
                  def is_vowel(word):
                      ''' Check if `word` is vowel, returns bool. '''
                      # Split word in two equals parts
                      if len(word) % 2 == 0:
                          parts = [word[0:len(word)/2], word[len(word)/2:]]
                      else:
                          parts = [word[0:len(word)/2], word[(len(word)/2)+1:]]
                  
                      # Check if first part and reverse second part are same.
                      if parts[0] == parts[1][::-1]:
                          return True
                      else:
                          return False
                  

                  【讨论】:

                    【解决方案11】:

                    这是一个智能解决方案,可帮助您计算和查找输入字符串中的元音:

                    name = input("Name:- ")
                    counter = []
                    list(name)
                    
                    for i in name:                 #It will check every alphabet in your string
                        if i in ['a','e','i','o','u']:  # Math vowels to your string
                            print(i,"  This is a vowel")
                            counter.append(i)      # If he finds vowels then he adds that vowel in empty counter
                        else:
                            print(i)
                    
                    print("\n")
                    print("Total no of words in your name ")
                    print(len(name))
                    
                    print("Total no of vowels in your name ")
                    print(len(counter))
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2017-11-14
                      • 1970-01-01
                      • 2013-11-27
                      • 2012-10-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2021-08-07
                      • 2020-08-19
                      相关资源
                      最近更新 更多