Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

python - 构建树:如何在python中格式化父级和子级列表(Build tree : how to format a parent and children list in python)

I need to change the format of a list following a tree structure where a parent can have multiple children (not a binary tree).

(我需要在一个树结构下更改一个列表的格式,在该树结构中,一个父母可以有多个孩子(而不是二叉树)。)

My list follow this structure: [an object, a player, my_id, my_parent_id]

(我的列表遵循以下结构: [an object, a player, my_id, my_parent_id])

What I want:

(我想要的是:)

[an object, a player, my_id, my_parent_id, [my children, [my 
    grandchildren, [...]]]]

I could make a program to format like this but I wanted to know if there are already any way to do this.

(我可以制作一个程序来格式化,但是我想知道是否已有任何方法可以这样做。)

Here is the output of my application:

(这是我的应用程序的输出:)

[[<__main__.Nim object at 0x013FDBD0>, 'Human', 0, 0], [<__main__.Nim object at 0x014CDAD0>, 'Bot', 1, 0], [<__main__.Nim object at 0x014CDAF0>, 'Bot', 2, 0], [<__main__.Nim object at 0x014CDB90>, 'Bot', 3, 0], [<__main__.Nim object at 0x014CDBD0>, 'Bot', 4, 0], [<__main__.Nim object at 0x014CDC50>, 'Bot', 5, 0], [<__main__.Nim object at 0x014CDC10>, 'Human', 6, 5]]

What I want :

(我想要的是 :)

[[<__main__.Nim object at 0x013FDBD0>, 'Human', 0, 0, [<__main__.Nim object at 0x014CDAD0>, 'Bot', 1, 0], [<__main__.Nim object at 0x014CDAF0>, 'Bot', 2, 0], [<__main__.Nim object at 0x014CDB90>, 'Bot', 3, 0], [<__main__.Nim object at 0x014CDBD0>, 'Bot', 4, 0], [<__main__.Nim object at 0x014CDC50>, 'Bot', 5, 0, [<__main__.Nim object at 0x014CDC10>, 'Human', 6, 5]]]

Thank you in advance.

(先感谢您。)

  ask by Sourou Cerulean translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...