Python 根据列表中每个元素的键创建树
我想根据列表中每个元素的键创建一棵树 逻辑结果将是:Python 根据列表中每个元素的键创建树,python,list,tree,wxpython,nested,Python,List,Tree,Wxpython,Nested,我想根据列表中每个元素的键创建一棵树 逻辑结果将是: >>> s [{'000000': [['apple', 'pear']]}, {'100000': ['good', 'bad']}, {'200000': ['yeah', 'ogg']}, {'300000': [['foo', 'foo']]}, {'310000': [['#'], ['#']]}, {'320000': ['$', ['1']]}, {'321000': [['abc', 'abc']]}, {'
>>> s
[{'000000': [['apple', 'pear']]}, {'100000': ['good', 'bad']}, {'200000': ['yeah', 'ogg']}, {'300000': [['foo', 'foo']]}, {'310000': [['#'], ['#']]}, {'320000': ['$', ['1']]}, {'321000': [['abc', 'abc']]}, {'322000': [['#'], ['#']]}, {'400000': [['yeah', 'baby']]}]
>>> for i in s:
... print i
...
{'000000': [['apple', 'pear']]}
{'100000': ['good', 'bad']}
{'200000': ['yeah', 'ogg']}
{'300000': [['foo', 'foo']]}
{'310000': [['#'], ['#']]}
{'320000': ['$', ['1']]}
{'321000': [['abc', 'abc']]}
{'322000': [['#'], ['#']]}
{'400000': [['yeah', 'baby']]}
也许嵌套列表可以实现这一点,或者我需要一个树类型?如果您只是想要一个python结构,您可以使用:
{'000000': [['apple', 'pear']]}
{'100000': ['good', 'bad']}
{'200000': ['yeah', 'ogg']}
{'300000': [['foo', 'foo']]}
{'310000': [['#'], ['#']]}
{'320000': ['$', ['1']]}
{'321000': [['abc', 'abc']]}
{'322000': [['#'], ['#']]}
{'400000': [['yeah', 'baby']]}
e、 g.在每个键值对中,存储一个元组作为值,这样元组的第一个元素将是节点数据([['apple','pear']]),元组的第二个元素将是节点的后代列表。这里有一种方法。我假设您可以依靠键来正确地表示树结构(没有不带“300000”的“310000”-这会导致问题,除非您在将缺少的节点添加到TreeCtrl时处理它们。) 我将首先重新组织数据,这样您就可以按键检索每个节点的关联数据,并在每个节点中存储一些附加信息
{'000000': ([['apple', 'pear']], [
{'100000': (['good', 'bad'], )},
{'200000': (['yeah', 'ogg'], )},
{'300000': ([['foo', 'foo']],[
{'310000': ([['#'], ['#']], )},
{'320000': (['$', ['1']],[
{'321000': ([['abc', 'abc']], )},
{'322000': ([['#'], ['#']], )}
])},
{'400000': ([['yeah', 'baby']], )}
])}
然后,通过将键的最后一个非零数字替换为零,然后将其填充回原始位数,遍历并计算出每个节点的父ID。使用父ID更新每个dict:
# original list of dicts
tree = [{'000000': [['apple', 'pear']]},
{'100000': ['good', 'bad']},
{'200000': ['yeah', 'ogg']},
{'300000': [['foo', 'foo']]},
{'310000': [['#'], ['#']]},
{'320000': ['$', ['1']]},
{'321000': [['abc', 'abc']]},
{'322000': [['#'], ['#']]},
{'400000': [['yeah', 'baby']]}]
# reorganize your data into a dict:
# {'000000': {'data':[['apple', 'pear']]},
# '100000': {'data':['good', 'bad']}, ...
tree = dict([(item.keys()[0], {'data':item[item.keys()[0]]})
for item in tree])
这使您可以很好地使用wx.TreeCtrl,因为每个节点现在都有一个对其父节点的引用:
for key in tree_dict.keys():
parent_id = key.strip('0')[:-1].ljust(len(key), '0')
# If it's all zeros, set it to None so we know the parent is root
if int(parent_id) == 0:
parent_id = None
tree_dict[key].update({'parent':parent_id})
将根节点添加到wx.TreeCtrl中,然后单步执行已排序的dict键,将每个项目中的数据添加到TreeCtrl中,以您希望的方式显示。对于您添加的每个项目,使用AppendItem()或InsertItem()返回的TreeItemId再次更新其dict。如果dict中的“parent”值为None,则知道父节点应该是根节点。如果不是,则使用parent值检索父节点的TreeItemId,该ID在将其添加到TreeCtrl时应已更新
我希望这是有意义的。嵌套Python数据结构工作得很好-您不需要树类型。但是一旦你有了这棵树,你打算怎么处理它呢?你真的需要它吗?难道你不能通过适当的键来查找东西吗?你是想得到这样的输出,还是想用逻辑来表示结果的对象?我想用wx.TreeCtrl来说明这一点。我不知道这棵“树”有多深,我怎样才能把它放到wx.TreeCtrl中呢
{'000000':{'data': [['apple', 'pear']], 'parent': None},
'100000':{'data': ['good', 'bad'], 'parent': None},
'200000':{'data': ['yeah', 'ogg'], 'parent': None},
'300000':{'data': [['foo', 'foo']], 'parent': None},
'310000':{'data': [['#'], ['#']], 'parent': '300000'},
'320000':{'data': ['$', ['1']], 'parent': '300000'},
'321000':{'data': [['abc', 'abc']], 'parent': '320000'},
'322000':{'data': [['#'], ['#']], 'parent': '320000'},
'400000':{'data': [['yeah', 'baby']], 'parent': None}}