简体   繁体   English

python 解析 n 叉树嵌套括号中的文本

[英]python parse text in nested parentheses for n-ary tree

I have text like that:我有这样的文字:

Proxy='ab,cd(ef,gh),ij,kl(mn(op,kr),st),uv' 

The expected result would be a nested list in order to create a nary tree representation of the text, so:预期的结果将是一个嵌套列表,以便创建文本的一元树表示,因此:

ExpectedResult=['ab','cd',['ef','gh'],'ij','kl',['mn',['op','kr'],'st'],'uv']

My try:我的尝试:

temp=[]
stack=[]
comma=[]
op=[]
cl=[]
n=(len(test))

for idx in range(n):
    if test[idx] == ',' and not op and not cl and not comma:
        stack.append(test[0:idx])
        comma.append(idx)
    elif test[idx] == ',' and op and not cl and not comma:
        temp.append(test[op.pop()+1:idx])
        comma.append(idx)
    elif test[idx] == ',' and not op and cl and not comma:
        if len(test[cl[0]+1:idx]) > 1:
            stack.append(test[cl.pop()+1:idx])
            comma.append(idx)
        else:
            cl.pop()
            comma.append(idx)
    elif test[idx] == ',' and not op and not cl and comma:
        stack.append(test[comma.pop():idx])
        comma.append(idx)
    elif test[idx] == '(' and not op and not cl and comma:
        stack.append(test[comma.pop()+1:idx])
        op.append(idx)
    elif test[idx] == '(' and op and not cl and comma:
        temp.append(test[comma.pop()+1:idx])
        op.pop()
        op.append(idx)
    elif test[idx] == ')' and not op and not cl and comma:
        temp.append(test[comma.pop()+1:idx])
        stack.append(temp)
        temp=[]
        cl.append(idx)
    elif test[idx] == ')' and op and not cl and not comma:
        temp.append([test[op.pop()+1:idx]])
        cl.append(idx)
    elif test[idx] == ')' and not op and not cl and comma:
        temp.append(test[comma.pop()+1:idx])
        stack.append(temp)
        temp=[]
        cl.append(idx)

I have found very interesting things here我在这里发现了非常有趣的东西

But this method will return a list of char and I want to join words (not 'a','b' but 'ab') and most of all I do not understand the syntax (and so the function) of the push function.但是这个方法会返回一个char列表,我想加入单词(不是'a','b'而是'ab'),最重要的是我不明白push function的语法(以及函数)。

As iit is said in the comment, here my solution that can be adapted to any sort of string.正如评论中所说,这里我的解决方案可以适应任何类型的字符串。 The trick is to transform the string given in input to look like a list and then use ast to effectively transform it into a list.诀窍是将输入中给出的字符串转换为看起来像一个列表,然后使用 ast 将其有效地转换为一个列表。

def parenthesesParser(string):
    ref=[]
    string=re.sub('\.','',string)
    string=string.rstrip(', ')
    for char in string:
        if char == '(':
            ref.append('\',[\'')
        elif char == ')':
            ref.append('\']')
        elif char == ',':
            ref.append('\',\'')
        elif char == '.':
            ref.append('\',\'')
        else:
            ref.append(char)
    ref='\''+''.join(ref)+'\''
    ref=re.sub('\'\'','\'',ref)
    ref=re.sub(']\'',']',ref)
   return ast.literal_eval(ref.strip())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM