Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # even_parentheses.py
- parentheses = {')':'(','}':'{',']':'['}
- def encl(sss, s=''):
- def peg(sss):
- t = len(sss)
- t,t2 = min([([sss.index(z),z] if z in sss else [t,'']) for z in '[({'])
- if t2:
- return sss[t:]
- return ''
- def seg():
- for n,s in enumerate(sss):
- if s == ',' and d == []:
- return sss[:n]
- elif s in '[({':
- d.append(s)
- elif s in '])}':
- try:
- d.remove(parentheses[s])
- except:
- return sss[:n]
- t,t2 = max([([sss.rindex(z),z] if z in sss else [0,'']) for z in '])}'])
- if t2:
- return sss[:t+1]
- return ''
- d = []
- if s:
- if s == 1:
- sss = peg(sss)
- elif s == 2:
- t = []
- while 1:
- sss = peg(sss)
- s = seg()
- sss = sss[s.index(s)+len(s):]
- if s:
- t.append(s)
- else:
- return t
- try:
- if ',' in s:
- sss = sss[sss.index(s)+len(s):]
- else:
- sss = sss[sss.index(s):]
- except:
- 0
- return seg()
- 0
- zzz = "test=[1,(2,[[3,4],{5,'6'},7],8),9],(xyz=({'A':('B','C')},['D','E'])),('ZZZ') ###"
- print encl(zzz)
- print encl(zzz,1)
- print encl(zzz,'2,')
- print encl(zzz,'xyz')
- print
- for z in encl(zzz,2):
- print z
- '''
- test=[1,(2,[[3,4],{5,'6'},7],8),9]
- [1,(2,[[3,4],{5,'6'},7],8),9]
- [[3,4],{5,'6'},7]
- xyz=({'A':('B','C')},['D','E'])
- [1,(2,[[3,4],{5,'6'},7],8),9]
- (xyz=({'A':('B','C')},['D','E']))
- ('ZZZ')
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement