根据列表中每个元素的键创建一棵树

发布于 2024-10-12 13:17:34 字数 1019 浏览 4 评论 0原文

>>> 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小嗲 2024-10-19 13:17:34

这是一种方法。我假设您可以依靠键来正确表示树结构(没有“310000”而没有“300000”——这会导致问题,除非您在将丢失的节点添加到 TreeCtrl 时处理它们。

)首先会重新组织数据,这样您就可以通过键检索每个节点的关联数据,并在每个节点中存储一些附加信息。

# original list of dicts
tree = [{'000000': [['apple', 'pear']]},
        {'100000': ['good', 'bad']},
        {'200000': ['yeah', 'ogg']},
        {'300000': [['foo', 'foo']]},
        {'310000': [['#'], ['#']]},
        {'320000': ['

然后通过将键的最后一个非零数字替换为零,然后将其填充回原始数字位数,找出每个节点的父 ID。使用父 ID 更新每个字典:

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,因为每个节点现在都有对其父级的引用:

{'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': ['

将根节点添加到您的 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 时应该已更新。

我希望这是有道理的。

, ['1']], 'parent': '300000'}, '321000':{'data': [['abc', 'abc']], 'parent': '320000'}, '322000':{'data': [['#'], ['#']], 'parent': '320000'}, '400000':{'data': [['yeah', 'baby']], 'parent': None}}

将根节点添加到您的 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.

# original list of dicts
tree = [{'000000': [['apple', 'pear']]},
        {'100000': ['good', 'bad']},
        {'200000': ['yeah', 'ogg']},
        {'300000': [['foo', 'foo']]},
        {'310000': [['#'], ['#']]},
        {'320000': ['

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:

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})

This sets you up nicely to use the wx.TreeCtrl, since each node now has a reference to its parent:

{'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': ['

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.

, ['1']], 'parent': '300000'}, '321000':{'data': [['abc', 'abc']], 'parent': '320000'}, '322000':{'data': [['#'], ['#']], 'parent': '320000'}, '400000':{'data': [['yeah', 'baby']], 'parent': None}}

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.

宫墨修音 2024-10-19 13:17:34

如果你只想要一个Python结构,你可以使用这个:

{'000000': ([['apple', 'pear']], [
  {'100000': (['good', 'bad'], )},
  {'200000': (['yeah', 'ogg'], )},
  {'300000': ([['foo', 'foo']],[
     {'310000': ([['#'], ['#']], )},
     {'320000': (['

例如在每个键值对中存储一个元组作为值,这样元组的第一个元素将是一个节点数据([['apple', 'pear'] ] 例如),元组的第二个元素将是节点后代的列表。

, ['1']],[ {'321000': ([['abc', 'abc']], )}, {'322000': ([['#'], ['#']], )} ])}, {'400000': ([['yeah', 'baby']], )} ])}

例如在每个键值对中存储一个元组作为值,这样元组的第一个元素将是一个节点数据([['apple', 'pear'] ] 例如),元组的第二个元素将是节点后代的列表。

If you just want a python structure, you may use this:

{'000000': ([['apple', 'pear']], [
  {'100000': (['good', 'bad'], )},
  {'200000': (['yeah', 'ogg'], )},
  {'300000': ([['foo', 'foo']],[
     {'310000': ([['#'], ['#']], )},
     {'320000': (['

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.

, ['1']],[ {'321000': ([['abc', 'abc']], )}, {'322000': ([['#'], ['#']], )} ])}, {'400000': ([['yeah', 'baby']], )} ])}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文