给定输入:
apple: banana eggplant
banana: cantaloupe durian
eggplant:
fig:
我想把它连接成以下格式:
^{pr2}$
可能有多个根元素,也可能没有多个根元素(在上面的示例中,有两个根元素),我希望找到一个能够毫无问题地处理它们的解决方案。在
有没有可以处理这种转换的命令行工具?如果做不到这一点,在其他脚本语言中是否有什么东西可以稍微轻松地处理这个问题(我已经看过Python的pprint
,但我也不确定如何使用它来处理类似的问题)?在
Tags:
这是另一个版本供你参考。在
我还从stdin添加了read文件,从而能够与bash脚本集成。你需要取消最后一部分的注释。假设这个脚本被称为脚本.py,您可以调用
python script.py < test.txt
来读取文件。这个文本.txt将存储您提供的文本内容。在这个问题很古老,但这里是第一个解决方案的networkx版本:
以下代码将生成您所要求的树结构:
它要求你用给定的形式表示数据。在
关于数据反序列化,您只需要跟踪父节点,这样当叶看起来是节点时,只需替换它:
^{pr2}$它运行:
希望它能完全解决你的问题。在
更新
我的代码对转角情况提供了一些关注,例如:
注意},因为它们被抑制了。在
apple
子节点的最左边,末尾没有{或者中间是空的:
相关问题 更多 >
编程相关推荐