Given string representation of a nested list tree, return a list containing all the deepest list contents. For example: '[[1,[2, 2a]],[[3,3b],4]]' ==> ['2, 2a', '3,3b'] '[[[1,[2, 2a]],[[3,3b],4]],6]' ==> ['2, 2a', '3,3b'] '[[[[a1,a2],out],o1],[o2,o3]]' ==> ['
(LString)
| 76 | |
| 77 | #======================================================================== |
| 78 | def deepList(LString): |
| 79 | ''' |
| 80 | Given string representation of a nested list tree, |
| 81 | return a list containing all the deepest list contents. |
| 82 | |
| 83 | For example: |
| 84 | |
| 85 | '[[1,[2, 2a]],[[3,3b],4]]' |
| 86 | ==> ['2, 2a', '3,3b'] |
| 87 | |
| 88 | '[[[1,[2, 2a]],[[3,3b],4]],6]' |
| 89 | ==> ['2, 2a', '3,3b'] |
| 90 | |
| 91 | '[[[[a1,a2],out],o1],[o2,o3]]' |
| 92 | ==> ['a1,a2', 'o2,o3'] |
| 93 | |
| 94 | '[[[[[a1,a2], out], [o1,o2]],[o3,o4]],[o5,o6]]' |
| 95 | ==> ['a1,a2', 'o1,o2', 'o3,o4', 'o5,o6'] |
| 96 | |
| 97 | The code: [x.split(']') for x in code.split('[')] |
| 98 | returns something like: |
| 99 | [[''], [''], [''], [''], [''], ['a1,a2', ', out', ', '], |
| 100 | ['o1,o2', '', ','], ['o3,o4', '', ','], ['o5,o6', '', '']] |
| 101 | |
| 102 | ''' |
| 103 | result= [x[0] for x in \ |
| 104 | [x.split(']') for x in LString.split('[')] \ |
| 105 | if len(x)>1] |
| 106 | if result==['']: result =[] |
| 107 | return result |
| 108 | |
| 109 | |
| 110 | #======================================================================== |