目前我对python编程很新,一直在研究它,我遇到了一个小项目任务,即制作一个程序,将所有元音都从一个语句中删除,所以我决定尝试一下。我想出了一个程序,但似乎它只取出元音有时,我发现这非常奇怪,我想请求一些帮助来解决它。
def anti_vowel(text):
list = ['a', 'e', 'i', 'o', 'u']
big_list = ['A', 'E', 'I', 'O', 'U']
list_word = []
for f in text:
list_word.append(f)
for vowel in list:
for letter in list_word:
if vowel == letter:
list_word.remove(vowel)
for vowel in big_list:
for letter in list_word:
if vowel == letter:
list_word.remove(vowel)
new_word = ''.join(list_word)
return new_word
print anti_vowel("uuuUUUUUIIIIiiiIiIoOuuooouuUOUUuooouU")
这个语句打印出'IiIuUUuoouU',但是如果我使用更多for语句在列表上添加更多迭代,它会减少显示的字母数量。有人可以告诉我为什么会这样吗?
答案 0 :(得分:0)
稍作改进:
list = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
def anti_vowel(text):
return ''.join([x for x in text if x not in list])
print anti_vowel("uuuUUUUUIIIIiiiIiIoOuuooouuUOUUuooouU")
答案 1 :(得分:0)
此代码是正确的实现
def anti_vowel(c):
newstr = c
vowels = ('a', 'e', 'i', 'o', 'u')
for x in c.lower():
if x in vowels:
newstr = newstr.replace(x,"")
return newstr
答案 2 :(得分:0)
Abend的解决方案几乎没有任何正确的编辑
vowels = ['a','e','i','o','u','A','E','I','O','U']
def no_vowels(str1):
return ''.join([char for char in list(str1) if char not in vowels])
print no_vowels('finished')
给出
fnshd
Process finished with exit code 0