#/usr/bin/env python
#-*- coding:utf-8 -*-
#思路:要先从最右边找‘(’;与之对应的‘)’要满足索引大于‘(’且最左边一个的索引;找到一个从‘)’的索引列表中去掉一个
#以如下表达式为例
#1+3+((5*5+6)+5)-6/3-(4+5)
a=‘1+3+((5*5+6)+5)-6/3-(4+5)‘
la=[]
lb=[]
for i in range(len(a)):
if a[i]==‘(‘:
la.append(i)
elif a[i]==‘)‘:
lb.append(i)
if len(la)!=len(lb):
print("Your expression are wrong with using symblic ‘(‘or ‘)‘")
else:
print("la:",sorted(la))
print("lb:",sorted(lb))
print("la|lb",set(la)|set(lb))
couple1=dict.fromkeys(la,None)
print(sorted(couple1.items(),key=lambda d :d[0],reverse=False))
for i in sorted(couple1.keys(),reverse=True):
for j in sorted(lb):
if j>i:
couple1[i]=j
lb.remove(j)
break
print("括号分隔后的索引片段:",sorted(couple1.items(),key=lambda d:d[0]))