我有点纠结于创建反ú元音定义:
定义一个名为anti_元音的函数,它接受一个字符串text作为输入,并返回去掉所有元音的文本
这是我的尝试:

def anti_vowel(text):
    vowels=["a","A","e","E","i","I","o","O","u","U"]
    text_input=[""]
    for char in text:
        text_input.append(char)
    av = [char for char in text_input if char not in vowels]
    return av

我的代码将输入作为单独的字符返回。
这是我得到的错误:
Oops, try again. Your function fails on anti_vowel("Hey look Words!"). It returns "['', 'H', 'y', ' ', 'l', 'k', ' ', 'W', 'r', 'd', 's', '!']" when it should return "Hy lk Wrds!".

有人能告诉我正确的方向吗?

最佳答案

考虑:

>>> tgt='This is some text with vowels'
>>> vowels='aeiou'
>>> ''.join(e for e in tgt if e.lower() not in vowels)
'Ths s sm txt wth vwls'

或者,正如注释中指出的,在join中使用实际的列表理解:
>>> ''.join([e for e in tgt if e.lower() not in vowels])
'Ths s sm txt wth vwls'

10-05 20:46
查看更多