def anti_vowel(text):
empL = [] #just an empty list
index = 0 #just an index counter
empS = "" #just an empty String
for i in text:
empL.append(i) # in this loop ill be adding the str passed in by "text" char by char to the empty list
else: # since this its a for/else loop this is also going to run
for char in empL: # iterates to each element in the list
if char in "aeiouAEIOU": **# if the element thats being iterated at the moment is cotained in this string**
empL.remove(char) #remove it
else: #yes this is going to run because it's another for/else
sizeEmpL = len(empL) # just the size of empty list
while sizeEmpL != 0 :
empS = empS + empL[index]
print(empS)
index += 1
sizeEmpL -= 1
print(empL)
print(empS)
所以基本上我应该传递一个字符串作为参数,prog应该在一个空列表中将char放入char并检查每个char与字符串“aeiouAEIOU”的比较,如果char比较包含任何一个“aeiouAEIOU”然后它应该删除它。 然后我将元素添加到空字符串并打印出无元音字符串
答案 0 :(得分:1)
在迭代时从列表中删除元素并不像预期的那样工作。迭代时不要修改列表。这是问题代码:
for char in empL:
if char in "aeiouAEIOU":
empL.remove(char)
在迭代列表时缩短列表会导致跳过列表中的其他字符。
答案 1 :(得分:0)
string = 'ofiiajpfeiajpfeiaef ijgapijfpij'
''.join([x for x in string if x not in ['a','e','i','o','u','A','E','I','O','U']])
输出:
'fjpfjpff jgpjfpj'
你可以发挥它的作用:
def no_vowels(input_string):
return (''.join([x for x in input_string if x not in ['a','e','i','o','u','A','E','I','O','U']]))
希望有所帮助