我正在尝试删除随机文本中的元音。
def disemvowel(string):
wowels = "aeiouAEIOU"
wowellist = list(wowels)
correctedList = list(string)
for i in correctedList:
for j in wowellist:
if i == j:
j = wowellist[0]
correctedList.remove(i)
print(i)
string = "".join(str(x) for x in correctedList)
return string
print(disemvowel("Your text wowel will be removed!"))
但是当删除第一个元音“o”时,第二个元音“u”不会被处理。我认为这是由于correctedList.remove(i)
但如何以另一种方式删除列表中的元素?
最佳答案
def disemvowel(string):
wowels = "aeiouAEIOU"
wowellist = list(wowels)
correctedList = list(string)
outlist=[x for x in correctedList if x not in wowels]
string = "".join(str(x) for x in outlist)
return string
print(disemvowel("Your text wowel will be removed!"))
关于python - 如果满足当前条件,For 循环不会处理下一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49124705/