在Stack Overflow上还有其他问题,例如how-to-parse-a-string-and-return-a-nested-array?
但它们都以((abc)de(fg)))
的格式引用列表。转到表单:[['a','b','c']'d','e'['f','g',]]]
我有一个表单列表:
((wordOneWord2)OtherWord(FinalWord)))
通过使用我从嵌套列表中的其他问题中学到的方法的形式为:
[['w','o','r','d','O','n','e','W','o','r','d','2']'O','t','h','e','r','W','o','r','d',['F','i','n','a','l','W','o','r','d']]]
而不是所需的
[['wordOneWord2'], 'OtherWord', ['FinalWord']]
我可以通过逐字母解析列表然后将每个列表中的项目连接在一起来实现所需的结果,但它需要的代码比我认为的要多,是否有更快的方法呢?
答案 0 :(得分:2)
import re
def parse_nested(text, left=r'[(]', right=r'[)]', sep=r','):
""" Based on https://stackoverflow.com/a/17141899/190597 (falsetru) """
pat = r'({}|{}|{})'.format(left, right, sep)
tokens = re.split(pat, text)
stack = [[]]
for x in tokens:
if not x or re.match(sep, x): continue
if re.match(left, x):
stack[-1].append([])
stack.append(stack[-1][-1])
elif re.match(right, x):
stack.pop()
if not stack:
raise ValueError('error: opening bracket is missing')
else:
stack[-1].append(x)
if len(stack) > 1:
print(stack)
raise ValueError('error: closing bracket is missing')
return stack.pop()
text = '((wordOneWord2)OtherWord(FinalWord))'
print(parse_nested(text))
# [[['wordOneWord2'], 'OtherWord', ['FinalWord']]]