Python:如何循环遍历树的所有未知深度?
我有一个战略问题,就是写一个程序来完成工作 我有如下CSV文件:Python:如何循环遍历树的所有未知深度?,python,json,Python,Json,我有一个战略问题,就是写一个程序来完成工作 我有如下CSV文件: Column1 Column 2 ------- ---------- parent1 [child1, child2, child3] parent2 [child4, child5, child6] child1 [child7, child8] child5 [child10, child33] ... ... 现在还不知道这些列表中的每个元素将扩展到多深,我想对它们进行循环 代码: def make
Column1 Column 2
------- ----------
parent1 [child1, child2, child3]
parent2 [child4, child5, child6]
child1 [child7, child8]
child5 [child10, child33]
... ...
现在还不知道这些列表中的每个元素将扩展到多深,我想对它们进行循环
代码:
def make_parentClass(self):
for i in self.csv_rows_list:
self.parentClassList.append(parentClass(i))
# after first Parent
for i in self.parentClassList:
if i.children !=[]:
for child in i.children:
for z in self.parentClassList:
if str(child) == str(z.node_parent):
i.node_children.append(z)
self.parentClassList.remove(z)
class parentClass():
node_children = []
def __init__(self, the_list):
self.node_parent = the_list[0]
self.children = the_list[1]
如果我能找到迭代的方法,上面的代码可能是一个解决方案。让我看看你是否喜欢这个问题,是否有意义
输出:
def make_parentClass(self):
for i in self.csv_rows_list:
self.parentClassList.append(parentClass(i))
# after first Parent
for i in self.parentClassList:
if i.children !=[]:
for child in i.children:
for z in self.parentClassList:
if str(child) == str(z.node_parent):
i.node_children.append(z)
self.parentClassList.remove(z)
class parentClass():
node_children = []
def __init__(self, the_list):
self.node_parent = the_list[0]
self.children = the_list[1]
我的目标是通过另一种语言构建treeview,但首先我需要以JSON格式输出。因此,预期的输出类似于:
{
paren1:{'child1':{'child7':{}, 'child8':{}},
'child2': {},
'child3': {},
},
parent2: {
'child4':{},
'child5': {
'child10':{},
'child33':{}
},
'child6':{}
}
}
我建议使用两个字典的解决方案。一个是嵌套的,其中包含您计划转换为JSON的实际数据结构,另一个是平面的,可以让您实际找到键。由于Python中的所有内容都是引用,因此可以确保两个字典的值完全相同。仔细修改平面词典将为您构建结构 下面的代码假设您已经成功地将每一行拆分为一个字符串
父项
和列表子项
,其中包含两列中的值
json_dict = {}
flat_dict = {}
for parent, children in file_iterator():
if parent in flat_dict:
value = flat_dict[parent]
else:
value = {}
flat_dict[parent] = json_dict[parent] = value
for child in children:
flat_dict[child] = value[child] = {}
运行此命令会产生如下的json\u dict
:
{
'parent1': {
'child1': {
'child7': {},
'child8': {}
},
'child2': {},
'child3': {}
},
'parent2': {
'child4': {},
'child5': {
'child10': {},
'child33': {}
},
'child6': {}
}
}
这里有一个可以使用的解决方案。我建议使用两个字典。一个是嵌套的,其中包含您计划转换为JSON的实际数据结构,另一个是平面的,可以让您实际找到键。由于Python中的所有内容都是引用,因此可以确保两个字典的值完全相同。仔细修改平面词典将为您构建结构 下面的代码假设您已经成功地将每一行拆分为一个字符串
父项
和列表子项
,其中包含两列中的值
json_dict = {}
flat_dict = {}
for parent, children in file_iterator():
if parent in flat_dict:
value = flat_dict[parent]
else:
value = {}
flat_dict[parent] = json_dict[parent] = value
for child in children:
flat_dict[child] = value[child] = {}
运行此命令会产生如下的json\u dict
:
{
'parent1': {
'child1': {
'child7': {},
'child8': {}
},
'child2': {},
'child3': {}
},
'parent2': {
'child4': {},
'child5': {
'child10': {},
'child33': {}
},
'child6': {}
}
}
这是一个可以玩的游戏。到目前为止,您尝试了什么?答案中发布的JSON与CSV不一致,您是否打算对每个孩子使用[]而不是{}?。此外,要处理每一项,只要在数据格式正确后使用递归函数即可。@h4z3我主要考虑的是解决它的策略。例如,涉及不可能的循环。我也在考虑编写一个函数,该函数可以循环回自身,但仍然无法说明它的外观。def process_item(self,item):if isinstance(item,list):for I in item:process_item(item)else:do_something_with_item(item)请提供您迄今为止尝试过的内容?答案中发布的JSON与CSV不一致,您是否打算对每个孩子使用[]而不是{}?。此外,要处理每一项,只要在数据格式正确后使用递归函数即可。@h4z3我主要考虑的是解决它的策略。例如,涉及不可能的循环。我也在考虑编写一个函数,该函数可以循环回自身,但仍然无法说明它的外观。def process_item(self,item):if isinstance(item,list):for I in item:process_item(item)else:do_something_与_item(item)一起请非常感谢。对我来说,这似乎是一个很好的解决方案。我要测试一下。有一种结构我必须处理,对我来说,每一行都是['parent',[child,child,child],但在你的解决方案中,内容是元组。这是一个优雅的回答,速度相当快,但有一个简单的问题,例如,如果Child1和它的子项是两个父项的输入,它的子项将只出现在其中一个父项中。例如,添加此项('parent3',['child1','child2','child3'])这是一个与您所问完全不同的问题。请继续选择此答案,然后选择另一个包含该条件的答案。好的,我知道了。我会的。谢谢。是的:)。也非常感谢您的帮助。非常感谢。对我来说,这实际上似乎是一个很好的解决方案。我将对它进行测试。有一个结构我必须处理,对我来说,每一行都是['parent',[child,child,child]但在您的解决方案中,内容是一个元组。对我来说,这是一个优雅的答案,速度相当快,但有一个简单的问题,例如,如果Child1及其子项由两个父项输入,则其子项将仅出现在其中一个父项中。例如,添加此('parent3'、['Child1'、'child2'、'child3'))这是一个与你所问完全不同的问题。继续选择这个答案,然后选择另一个包含该条件的答案。好的,我明白了。我会的。谢谢。是的:)。非常感谢你的帮助。