gpt4 book ai didi

python - 将嵌套括号树转换为嵌套列表

转载 作者:太空宇宙 更新时间:2023-11-03 11:30:02 25 4
gpt4 key购买 nike

我有一个树结构文件,其中括号用来表示树。这是将其转换为 Python 嵌套列表的代码

def foo(s):
def foo_helper(level=0):
try:
token = next(tokens)
except StopIteration:
if level != 0:
raise Exception('missing closing paren')
else:
return []
if token == ')':
if level == 0:
raise Exception('missing opening paren')
else:
return []
elif token == '(':
return [foo_helper(level+1)] + foo_helper(level)
else:
return [token] + foo_helper(level)
tokens = iter(s)
return foo_helper()

how to parse a strign and return nested array .

这里它工作正常,当字符长度为 1 时。对于单词或句子,同样不能正常工作。我的树样本是:

( Satellite (span 69 74) (rel2par Elaboration)
( Nucleus (span 69 72) (rel2par span)
( Nucleus (span 69 70) (rel2par span)
( Nucleus (leaf 69) (rel2par span) (text _!MERRILL LYNCH READY ASSETS TRUST :_!) )
( Satellite (leaf 70) (rel2par Elaboration) (text _!8.65 % ._!) )
)
( Satellite (span 71 72) (rel2par Elaboration)
( Nucleus (leaf 71) (rel2par span) (text _!Annualized average rate of return_!) )
( Satellite (leaf 72) (rel2par Temporal) (text _!after expenses for the past 30 days ;_!) )
)
)
( Satellite (span 73 74) (rel2par Elaboration)
( Nucleus (leaf 73) (rel2par span) (text _!not a forecast_!) )
( Satellite (leaf 74) (rel2par Elaboration) (text _!of future returns ._!) )
)
)

这里,输出需要是['satellite',['span','69','74'].........] 但是对于给定的函数,我得到的是 ['s ','a',''......['s','p','a','n','7','3']。 ...............]

如何修改?

最佳答案

我假设您想使用 _! 来表示带空格的字符串。然后我使用正则表达式拆分表达式:

from re import compile
resexp = compile(r'([()]|_!)')

tokens = iter(resexp.split(s))

我的结果是(使用深度为 4 的 pprint)

$ python lispparse.py  | head
['\n',
[' Satellite ',
['span 69 74'],
' ',
['rel2par Elaboration'],
'\n ',
[' Nucleus ',
['span 69 72'],
' ',
['rel2par span'],

我进一步完善了它:

tokens = iter(filter(None, (i.strip() for i in resexp.split(s))))

得到:

$ python lispparse.py  
[['Satellite',
['span 69 74'],
['rel2par Elaboration'],
['Nucleus',
['span 69 72'],
['rel2par span'],
['Nucleus', [...], [...], [...], [...]],
['Satellite', [...], [...], [...], [...]]],
['Satellite',
['span 73 74'],
['rel2par Elaboration'],
['Nucleus', [...], [...], [...]],
['Satellite', [...], [...], [...]]]]]

关于python - 将嵌套括号树转换为嵌套列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23025282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com