根据列表中每个元素的键创建一棵树
>>> 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']]}
我想根据列表中每个元素的键创建一棵树。
逻辑结果将是:
{'000000': [['apple', 'pear']]}
{'100000': ['good', 'bad']}
{'200000': ['yeah', 'ogg']}
{'300000': [['foo', 'foo']]}
{'310000': [['#'], ['#']]}
{'320000': ['$', ['1']]}
{'321000': [['abc', 'abc']]}
{'322000': [['#'], ['#']]}
{'400000': [['yeah', 'baby']]}
也许嵌套列表可以实现这个或者我需要一个树类型?
>>> s
[{'000000': [['apple', 'pear']]}, {'100000': ['good', 'bad']}, {'200000': ['yeah', 'ogg']}, {'300000': [['foo', 'foo']]}, {'310000': [['#'], ['#']]}, {'320000': ['
i want to make a tree based on the key of each element in list.
result in logic will be:
{'000000': [['apple', 'pear']]}
{'100000': ['good', 'bad']}
{'200000': ['yeah', 'ogg']}
{'300000': [['foo', 'foo']]}
{'310000': [['#'], ['#']]}
{'320000': ['
perhaps a nested list can implement this or I need a tree type?
, ['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': ['
i want to make a tree based on the key of each element in list.
result in logic will be:
perhaps a nested list can implement this or I need a tree type?
, ['1']]}
{'321000': [['abc', 'abc']]}
{'322000': [['#'], ['#']]}
{'400000': [['yeah', 'baby']]}
i want to make a tree based on the key of each element in list.
result in logic will be:
perhaps a nested list can implement this or I need a tree type?
, ['1']]}
{'321000': [['abc', 'abc']]}
{'322000': [['#'], ['#']]}
{'400000': [['yeah', 'baby']]}
perhaps a nested list can implement this or I need a tree type?
, ['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': ['i want to make a tree based on the key of each element in list.
result in logic will be:
perhaps a nested list can implement this or I need a tree type?
, ['1']]} {'321000': [['abc', 'abc']]} {'322000': [['#'], ['#']]} {'400000': [['yeah', 'baby']]}i want to make a tree based on the key of each element in list.
result in logic will be:
perhaps a nested list can implement this or I need a tree type?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一种方法。我假设您可以依靠键来正确表示树结构(没有“310000”而没有“300000”——这会导致问题,除非您在将丢失的节点添加到 TreeCtrl 时处理它们。
)首先会重新组织数据,这样您就可以通过键检索每个节点的关联数据,并在每个节点中存储一些附加信息。
将根节点添加到您的 wx.TreeCtrl,然后单步执行已排序的 字典键,将每个项目的数据添加到 TreeCtrl,无论您希望如何显示。对于添加的每个项目,使用 AppendItem() 或 InsertItem() 返回的 TreeItemId 再次更新其字典。如果字典中的“parent”值为 None,则您知道父节点应该是根节点。如果不是,请使用父值来检索父节点的 TreeItemId,该父节点在将其添加到 TreeCtrl 时应该已更新。
我希望这是有道理的。
, ['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])然后通过将键的最后一个非零数字替换为零,然后将其填充回原始数字位数,找出每个节点的父 ID。使用父 ID 更新每个字典:
这可以很好地设置您使用 wx.TreeCtrl,因为每个节点现在都有对其父级的引用:
将根节点添加到您的 wx.TreeCtrl,然后单步执行已排序的 字典键,将每个项目的数据添加到 TreeCtrl,无论您希望如何显示。对于添加的每个项目,使用 AppendItem() 或 InsertItem() 返回的 TreeItemId 再次更新其字典。如果字典中的“parent”值为 None,则您知道父节点应该是根节点。如果不是,请使用父值来检索父节点的 TreeItemId,该父节点在将其添加到 TreeCtrl 时应该已更新。
我希望这是有道理的。
Here's one approach. I'm making the assumption that you can rely on your keys to represent the tree structure properly (no '310000' without '300000' -- that would cause problems, unless you handle missing nodes when you add them to your TreeCtrl.)
I would start by reorganizing the data, so you can retrieve the associated data for each node by key, and also store some additional information in each node.
Add the root node to your wx.TreeCtrl, and then step through the sorted dict keys, adding the data from each item to the TreeCtrl, however you want it displayed. And for each item you add, update its dict again with the TreeItemId returned by AppendItem() or InsertItem(). If the 'parent' value in the dict is None, you know the parent should be the root node. If it's not, use the parent value to retrieve the TreeItemId of the parent node, which should have been updated when you added it to the TreeCtrl.
I hope that makes sense.
, ['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])Then go through and figure out the parent ID for each node by replacing the last non-zero digit of the key with zero, and then padding it back to the original number of digits. Update each dict with the parent ID:
This sets you up nicely to use the wx.TreeCtrl, since each node now has a reference to its parent:
Add the root node to your wx.TreeCtrl, and then step through the sorted dict keys, adding the data from each item to the TreeCtrl, however you want it displayed. And for each item you add, update its dict again with the TreeItemId returned by AppendItem() or InsertItem(). If the 'parent' value in the dict is None, you know the parent should be the root node. If it's not, use the parent value to retrieve the TreeItemId of the parent node, which should have been updated when you added it to the TreeCtrl.
I hope that makes sense.
如果你只想要一个Python结构,你可以使用这个:
例如在每个键值对中存储一个元组作为值,这样元组的第一个元素将是一个节点数据([['apple', 'pear'] ] 例如),元组的第二个元素将是节点后代的列表。
If you just want a python structure, you may use this:
e.g. in every key-value pair store a tuple as a value so that the first element of the tuple would be a node data ([['apple', 'pear']] for example) and the second element of the tuple would be a list of node's descendants.