3

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

4

11 回答 11

7

也许

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)
于 2013-06-06T20:21:22.353 回答
6

你可以用正则表达式做到这一点:

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

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

于 2013-06-06T20:22:17.640 回答
1
''.join(['up' + v if v.lower() in 'aeiou' else v for v in phrase])
于 2013-06-06T20:24:53.410 回答
0
for vowel in [“a“,“e“,“i“,“o“,“u“]:
    Word = Word.replace(vowel,“up“+vowel)
print(Word)
于 2013-06-06T20:23:00.237 回答
0
import re
sentence = "whatever"
q = re.sub(r"([aieou])", r"up\1", sentence, flags=re.I)
于 2013-06-06T20:23:16.787 回答
0
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
于 2013-06-06T20:23:24.537 回答
0

我可能会使用 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'
>>> 
于 2013-06-07T17:14:29.540 回答
0

这是整个问题的一条线

>>> "".join(('up' + x if x.upper() in 'AEIOU' else x for x in 'andrew'))
'upandrupew'
于 2013-06-06T20:30:07.577 回答
0

这是一种方法。

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())))
于 2013-06-06T20:31:07.453 回答
0

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

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))
于 2018-11-28T09:56:38.423 回答
0
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
于 2017-07-13T09:41:03.270 回答