if语句中的行继续符后面的意外字符

时间:2014-12-17 07:11:49

标签: python if-statement

这是我从给定字符串中删除元音的代码。我的代码在if语句条件中显示行后续字符后的意外字符?可以有人帮忙

def anti_vowel(text):
r=len(text)
new=[]
for i in range(0,r):
if lower.text[i]!='a' and lower.text[i]!='e'
    and lower.text[i]!='i' and  lower.text[i]!='o' and lower.text[i]!='u':
    new.append(text[i])
print " ".join(new)`

2 个答案:

答案 0 :(得分:0)

我修改了你的函数以使用set和list comprehension:

def anti_vowel(text):            
    vowels = {'a', 'e', 'i', 'o', 'i'}
    new = [a_letter for a_letter in text if a_letter not in vowels]                    
    return "".join(new)    

print(anti_vowel("Hey You!"))
#prints: Hy Yu!

答案 1 :(得分:0)

我只是试图用这个证明你的工作 - 那是你的想法吗?

text="The brown fox quickly jumped over the lazy dog"
list_of_vow = ['a','e','i','o','u']
new_word = text;

for x in list_of_vow:
    new_word = new_word.replace(x,'')
print(new_word)