Advertisement
here2share

# even_parentheses.py

Feb 1st, 2020
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.33 KB | None | 0 0
  1. # even_parentheses.py
  2.  
  3. parentheses = {')':'(','}':'{',']':'['}
  4. def encl(sss, s=''):
  5.     def peg(sss):
  6.         t = len(sss)
  7.         t,t2 = min([([sss.index(z),z] if z in sss else [t,'']) for z in '[({'])
  8.         if t2:
  9.             return sss[t:]
  10.         return ''
  11.     def seg():
  12.         for n,s in enumerate(sss):
  13.             if s == ',' and d == []:
  14.                 return sss[:n]
  15.             elif s in '[({':
  16.                 d.append(s)
  17.             elif s in '])}':
  18.                 try:
  19.                     d.remove(parentheses[s])
  20.                 except:
  21.                     return sss[:n]
  22.         t,t2 = max([([sss.rindex(z),z] if z in sss else [0,'']) for z in '])}'])
  23.         if t2:
  24.             return sss[:t+1]
  25.         return ''
  26.     d = []
  27.     if s:
  28.         if s == 1:
  29.             sss = peg(sss)
  30.         elif s == 2:
  31.             t = []
  32.             while 1:
  33.                 sss = peg(sss)
  34.                 s = seg()
  35.                 sss = sss[s.index(s)+len(s):]
  36.                 if s:
  37.                     t.append(s)
  38.                 else:
  39.                     return t
  40.         try:
  41.             if ',' in s:
  42.                 sss = sss[sss.index(s)+len(s):]
  43.             else:
  44.                 sss = sss[sss.index(s):]
  45.         except:
  46.             0
  47.     return seg()
  48. 0
  49.  
  50. zzz = "test=[1,(2,[[3,4],{5,'6'},7],8),9],(xyz=({'A':('B','C')},['D','E'])),('ZZZ') ###"
  51.  
  52. print encl(zzz)
  53. print encl(zzz,1)
  54. print encl(zzz,'2,')
  55. print encl(zzz,'xyz')
  56.  
  57. print
  58. for z in encl(zzz,2):
  59.     print z
  60.  
  61. '''
  62. test=[1,(2,[[3,4],{5,'6'},7],8),9]
  63. [1,(2,[[3,4],{5,'6'},7],8),9]
  64. [[3,4],{5,'6'},7]
  65. xyz=({'A':('B','C')},['D','E'])
  66.  
  67. [1,(2,[[3,4],{5,'6'},7],8),9]
  68. (xyz=({'A':('B','C')},['D','E']))
  69. ('ZZZ')
  70. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement