如何使用函数创建父/子对象的嵌套列表?



我是Python和Django的新手,我遇到了一个问题,我不能自己解决很长一段时间

我有一个数据库,由一个数字对象和每个对象有一个父(除了第一个,"根"对象)和没有/一个/几个孩子。我需要创建一个函数,它接受一些对象(例如States在提供的情况下),并返回这个对象和递归的后代,像这样:

arr = ['States',
['Kansas', ['Lawrence', 'Topeka', ['Some Topeka Street', ['Some House on Some Topeka Street']]],
'Illinois', ['Chicago', 'Springfield']]]

我想获得这种数据结构,并在Django的无序列表过滤器中使用它。

根据我对算法的了解,我应该编一个这样的函数:

def make_nested_list(item):
if not item.children: # basic case
# do something
else:
for child in item.children: # recursive case
# do something
make_nested_list(child) # function calls itself
# maybe do something here
# and finally return self-nested list
return result 

我做的每件事都是一团糟。我该怎么做呢?

乌利希期刊指南:下面是我使用的模型:

class Thing(models.Model):
title = models.CharField(max_length=30)
parent = models.ForeignKey('self', on_delete=models.CASCADE, blank=True,
null=True)
def __str__(self):
return self.title
@property
def children(self):
return self.thing_set.all()
@property
def siblings(self):
return Thing.objects.filter(parent=self.parent)

假设您的item对象具有title属性,您可以这样做:

def list_nodes(siblings):
for node in siblings:
yield node.title
if node.children:
yield list(list_nodes(node.children))

这个函数的参数是一个list的节点。如果只有一个根对象,那么首先将其放入一个列表中,并将其作为参数传递:

result = list(list_nodes([root]))
下面是我执行的测试,其中包括模拟数据的创建:
from collections import namedtuple
Node = namedtuple("Node", "title, children")
root = Node("States", [
Node("Kansas", [
Node("Lawrence", []), 
Node("Topeka", [])
]), 
Node("Illinois", [])
])
print(root)
def list_nodes(siblings):
for node in siblings:
yield node.title
if node.children:
yield list(list_nodes(node.children))
result = list(list_nodes([root]))
print(result)

输出(应用了一些格式):

Node(title='States', children=[
Node(title='Kansas', children=[
Node(title='Lawrence', children=[]), 
Node(title='Topeka', children=[])
]),
Node(title='Illinois', children=[])
])
['States', ['Kansas', ['Lawrence', 'Topeka'], 'Illinois']]

最新更新