簡體   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] = ""

在上面的代碼中,程序檢查字母“r”是否在字符串中(在變量's'中表示,如果是r,則檢查此r之前的字母是否是元音,如果是元音被移除。例如

s = iterate
s = itrate

但是如果字符串中有多個'r',我如何讓程序執行此操作?

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