Python反元音函数-无法删除某些元音
我刚开始在科德学院学习Python。我试图做反元音功能,但发现“u”有以下问题Python反元音函数-无法删除某些元音,python,python-2.7,python-3.x,Python,Python 2.7,Python 3.x,我刚开始在科德学院学习Python。我试图做反元音功能,但发现“u”有以下问题 def anti_vowel(text): a = [] for i in text: a.append(i) for item in a: if item in "aeiouAEIOU": a.remove(item) print ''.join(a) print anti_vowel("Hey You!") print an
def anti_vowel(text):
a = []
for i in text:
a.append(i)
for item in a:
if item in "aeiouAEIOU":
a.remove(item)
print ''.join(a)
print anti_vowel("Hey You!")
print anti_vowel("Hey look Words!")
print anti_vowel("aeiouAEIOU")
它是印刷的
"Hy Yu!"
"Hy lk Words!"
"eoAIU"
而不是
"Hy Y!"
"Hy lk Wrds!"
""
不知何故,有些元音并没有被去掉
我找到了许多函数的替代方案。
但是,请帮助我识别当前代码的错误。当您仔细查看剩余的元音时,您可以看到,所有这些元音都是紧随其后的元音。在上一个示例中,a(已删除)e(保持)i(已删除)o(保持)等
def anti_vowel(text):
a = []
for i in text:
a.append(i)
b = a[:]
for item in a:
if item in "aeiouAEIOU":
b.remove(item)
print (''.join(b))
print (anti_vowel("Hey You!"))
print (anti_vowel("Hey look Words!"))
print (anti_vowel("aeiouAEIOU"))
这是因为您在对列表进行迭代的同时也在修改它
要解决这个问题,你应该复制一份清单。然后,您可以在修改副本的同时对原始元音进行迭代。当您仔细查看剩余的元音时,您可以看到所有这些元音都是紧随其后的元音。在上一个示例中,a(已删除)e(保持)i(已删除)o(保持)等 这是因为您在对列表进行迭代的同时也在修改它
要解决这个问题,你应该复制一份清单。然后,您可以在修改副本时对原始副本进行迭代。在迭代时删除项目不是一个好主意 将生成器理解传递给
str.join
def anti_vowel(text):
return ''.join(item for item in text if item not in "aeiouAEIOU")
或者使用集合
进行更快的字母查找(不确定转换为小写是否会加快查找速度,因为它会为此创建一个新字符串)
在迭代时删除项不是一个好主意 将生成器理解传递给
str.join
def anti_vowel(text):
return ''.join(item for item in text if item not in "aeiouAEIOU")
或者使用集合
进行更快的字母查找(不确定转换为小写是否会加快查找速度,因为它会为此创建一个新字符串)
不需要使用remove,也不需要迭代两次。相反,在迭代时,检查该项是否为元音,如果不是,则仅附加
def anti_vowel(text):
a = []
for i in text:
if i not in "aeiouAEIOU":
a.append(i)
print ''.join(a)
不需要使用remove,也不需要迭代两次。相反,在迭代时,检查该项是否为元音,如果不是,则仅附加
def anti_vowel(text):
a = []
for i in text:
if i not in "aeiouAEIOU":
a.append(i)
print ''.join(a)
为什么不像这样递归地尝试呢
def anti_vowel(s):
if not s:
return s
elif s[0] in "aeiouAEIOU":
return anti_vowel(s[1:])
return s[0] + anti_vowel(s[1:])
print (anti_vowel("Hey You!"))
print (anti_vowel("Hey look Words!"))
print (anti_vowel("aeiouAEIOU"))
输出:
Hy Y!
Hy lk Wrds!
为什么不像这样递归地尝试呢
def anti_vowel(s):
if not s:
return s
elif s[0] in "aeiouAEIOU":
return anti_vowel(s[1:])
return s[0] + anti_vowel(s[1:])
print (anti_vowel("Hey You!"))
print (anti_vowel("Hey look Words!"))
print (anti_vowel("aeiouAEIOU"))
输出:
Hy Y!
Hy lk Wrds!
您正在遍历时从列表中删除项,因此这会产生问题。 此外,也没有必要穿越那么多次 您可以使用:
def anti_vowel(text):
a = []
for i in text:
if i not in "aeiouAEIOU":
a.append(i)
print ''.join(a)
或者我会说,不要使用列表,而是使用如下字符串:
def anti_vowel(text):
a = ""
for item in text:
if item not in "aeiouAEIOU":
a+=item
print a
编辑:
正如@jean在评论部分指出的那样,使用
'.join(list)
比使用字符串cancatenation要快。您在遍历时正在从列表中删除项,因此这会产生问题。
此外,也没有必要穿越那么多次
您可以使用:
def anti_vowel(text):
a = []
for i in text:
if i not in "aeiouAEIOU":
a.append(i)
print ''.join(a)
或者我会说,不要使用列表,而是使用如下字符串:
def anti_vowel(text):
a = ""
for item in text:
if item not in "aeiouAEIOU":
a+=item
print a
编辑:
正如@jean在评论部分指出的那样,使用
'.join(list)
比使用字符串cancatenation要快。就像其他人已经说过的那样,您在遍历列表时正在修改列表。我确实想为这个任务推荐一个python内置选项,不过对于3.x>:
打印“你好!”。翻译(无,“aeiouAEIOU”)
在中,您需要考虑标准Unicode字符串并首先对其进行翻译:
translation = dict.fromkeys(map(ord, "aeiouAEIOU"), None)
print("Hey You!".translate(translation))
就像其他人已经说过的那样,您在迭代列表时正在修改列表。我确实想为这个任务推荐一个python内置选项,不过对于3.x>:
打印“你好!”。翻译(无,“aeiouAEIOU”)
在中,您需要考虑标准Unicode字符串并首先对其进行翻译:
translation = dict.fromkeys(map(ord, "aeiouAEIOU"), None)
print("Hey You!".translate(translation))
您还可以加入列表理解的结果,如:
def anti_vowel(text):
return "".join([char for char in text if char not in "aeiouAEIOU"])
如果您希望函数执行的不仅仅是打印到终端(例如,如果您希望以某种方式使用返回的值),请确保让函数返回其结果。如果函数打印其结果并返回None
(如果函数中没有return
语句),则无需打印该函数的结果
列表理解的
[char for char in text…]
部分只是名为text
的字符串中字符的for循环。[…如果字符不在“aeiouAEIOU”中]
排除了“aeiouAEIOU”
字符串中存在的字符。最后,“”.join([…])
将未排除的字符合并在一起,形成一个字符串,通过return
语句返回该字符串。您还可以将列表理解的结果合并,如:
def anti_vowel(text):
return "".join([char for char in text if char not in "aeiouAEIOU"])
如果您希望函数执行的不仅仅是打印到终端(例如,如果您希望以某种方式使用返回的值),请确保让函数返回其结果。如果函数打印其结果并返回None
(如果函数中没有return
语句),则无需打印该函数的结果
列表理解的
[char for char in text…]
部分只是名为text
的字符串中字符的for循环。[…如果字符不在“aeiouAEIOU”中]
排除了“aeiouAEIOU”
字符串中存在的字符。最后,“”.join([…])
将未排除的字符合并在一起,形成一个字符串,通过return
语句返回该字符串。在迭代列表时不要从列表中删除项,这就是导致您的奇怪错误的原因。而是从头开始创建一个缺少项目的新列表。另请参阅上的解释和链接问题。如果您现在正在学习Python,那么您应该学习Python 3。Python 2在2020年后将不再受支持。在遍历列表时不要从列表中删除项目,这就是导致此问题的原因