如果当前条件满足,则循环不处理下一个元素[重复]

问题描述 投票:1回答:1

这个问题在这里已有答案:

我试图用随机文本删除元音。

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)但我怎么能以另一种方式删除列表中的元素?

python
1个回答
2
投票
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!"))
© www.soinside.com 2019 - 2024. All rights reserved.