简体   繁体   English

如何在python中多次查找字符串中的字符位置?

[英]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 在上面的代码中,程序检查字母“r”是否在字符串中(在变量's'中表示,如果是r,则检查此r之前的字母是否是元音,如果是元音被移除。例如

s = iterate
s = itrate

But how do I make the program do this if there are multiple 'r's in the string? 但是如果字符串中有多个'r',我如何让程序执行此操作? ie

s = characters
s = chractrs
import re

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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