Python 将字典展平为格式化字符串
我在这个问题上纠缠了很长时间 假设我们有一个python字典,如下所示:Python 将字典展平为格式化字符串,python,dictionary,nlp,parse-tree,Python,Dictionary,Nlp,Parse Tree,我在这个问题上纠缠了很长时间 假设我们有一个python字典,如下所示: d = {'TOP': [{'S': [{'NP-TMP': [{'NP': [{'DT': ['This']}, {'NN': ['time']}]}, {'ADVP': [{'RP': ['around']}]}]}, {'NP-SBJ': [{'PRP': ['they']}]}, {'VP': [{'VBP': [&qu
d = {'TOP': [{'S': [{'NP-TMP': [{'NP': [{'DT': ['This']}, {'NN': ['time']}]},
{'ADVP': [{'RP': ['around']}]}]},
{'NP-SBJ': [{'PRP': ['they']}]},
{'VP': [{'VBP': ["'re"]},
{'VP': [{'VBG': ['moving']},
{'ADVP': [{'RB': ['even']},
{'RBR': ['faster']}]}]}]}]}]}
我想将其转换为以下格式:
(((((This)(time))((around)))((they))(('re)((moving)((even)(faster))))))
我尝试了以下代码,但无法继续
for tree in d.values():
for i, j in tree[0].items():
for n in j:
for p in n.items():
print(p)
递归生成器可以工作(虽然不是很好…) 结果是:
r = f"({''.join(sub(d))})"
print(r)
# (((((This)(time))((around)))((they))(('re)((moving)((even)(faster))))))
谢谢取消删除它:)
r = f"({''.join(sub(d))})"
print(r)
# (((((This)(time))((around)))((they))(('re)((moving)((even)(faster))))))