简体   繁体   中英

How to find position of character in a string multiple times in python?

for item in s:
    if item == "r":
        s = list(s)
        posR = s.index("r")
        while s[posR-1] == "a" or s[posR-1] == "e"\
           or  s[posR-1] == "i" or s[posR-1] == "o"\
           or s[posR-1] == "u":
            s[posR-1] = ""

In the code above, the program checks if the letter "r" is in a string (represented in the variable 's' and if it is r, then checks if the letter before this r is a vowel, and if it is the vowel is removed. eg

s = iterate
s = itrate

But how do I make the program do this if there are multiple 'r's in the string? ie

s = characters
s = chractrs
import re

s = 'iterater'
re.sub('[aeiou]r', 'r', s)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM