小编Ars*_*aba的帖子

在python中反转字符串中的子字符串

我正在编写一个程序来反转 python 中括号内的子字符串。结果字符串不应包含任何括号。我正在打印 b1 和 b2 以及 ch 以进行测试。似乎在 while 循环内的 for 循环的第二次迭代中,b1 变量未使用正确的索引进行更新。我尝试编写如下解决方案:

def reverseParentheses(s):
    r = s
    sstring = ''
    astring = ''
    b1 = b2 = 0
    count = 0
    for ch in s:
        if ch == '(':
            count+=1
        elif ch ==')':
            count+=1
        else:
            pass

    while True:
        b1 = b2 = 0
        for ch in r:
            if ch == '(':
                b1 = r.index(ch)
                print("b1= ",b1, ch)
            if ch == ')':
                b2 = r.index(ch)
                print("b2= ",b2, ch)
                sstring …
Run Code Online (Sandbox Code Playgroud)

python substring python-3.x

4
推荐指数
1
解决办法
784
查看次数

标签 统计

python ×1

python-3.x ×1

substring ×1