python - 如果满足当前条件,For 循环不会处理下一个元素

标签 python

我正在尝试删除随机文本中的元音。

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/

相关文章:

python - 将单独的 pandas 数据框中的列相乘

python - 使用 python 脚本更新 XML

python - Google AppEngine 应用程序版本

python - Django Manager 的顺序会影响哪些功能?

python - 渲染列表对象时出现问题 : AttributeError: 'list' object has no attribute '...'

python - sql server函数 native 参数绑定(bind)错误

python - 发电机 : KeyConditionExpression to check if an attribute exists or not null

python - 在 Pandas 中,如何在单个单元格中设置数字格式?

python - Pyo server.boot() 在 Ubuntu 14.04 上因 pyolib._core.PyoServerStateException 而失败

python - 使用 BeautifulSoup 选择所有 div sibling