MCPcopy Create free account
hub / github.com/ActiveState/code / deepList

Function deepList

recipes/Python/278257_List_Tools/recipe-278257.py:78–107  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

76
77#========================================================================
78def 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#========================================================================

Callers

nothing calls this directly

Calls 1

splitMethod · 0.45

Tested by

no test coverage detected