def anti_vowel - codecademy(python) [英] def anti_vowel - codecademy (python)

查看:93
本文介绍了def anti_vowel - codecademy(python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


定义一个名为anti_vowel的函数,它接受一个字符串text作为输入并返回删除了所有元音的文本。


这是我的尝试:

  def anti_vowel(text):
vowels = [a,A,e,E,i,I对于文本中的字符
text_input.append(char)
av =$
text_input = [] [char for char in text_input if char not in vowels]
return av

我的代码返回输入为单独的字符。



这是我得到的错误:

 糟糕,请再试一次。您的函数在anti_vowel上失败(Hey look Words!)。它返回['','H','y','','l','k','','W','r','d','s','!']当它应该返回Hy lk Wrds!。 

有人可以请我指出正确的方向吗?

 >>> =h2_lin>解决方案

tgt ='这是一些带元音的文字'
>>>元音='aeiou'
>>> ''.join(e for tgt if e.lower()not in vowels)
'Ths smtxt wth vwls'






或者,如评论中所指出的,使用实际列表理解加入是更好的

 > >> ''.join([e for tgt if e.lower()not in vowels])
's s sm txt wth vwls'


I am a bit stuck on creating the anti_vowel definition:

Define a function called anti_vowel that takes one string, text, as input and returns the text with all of the vowels removed

This is my attempt:

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

My code returns the input as separate characters.

This is the error I get:

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!". 

Could someone please point me in the right direction?

解决方案

Consider:

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


Or, as pointed out in comments, using an actual list comprehension inside join is better:

>>> ''.join([e for e in tgt if e.lower() not in vowels])
'Ths s sm txt wth vwls'

这篇关于def anti_vowel - codecademy(python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆