make a tree based on the key of each element in list
>>> 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']]}
i want to make a tree based on the key of each element in list.
result in logic will be:
{'000000': [['apple', 'pear']]}开发者_如何学JAVA
{'100000': ['good', 'bad']}
{'200000': ['yeah', 'ogg']}
{'300000': [['foo', 'foo']]}
{'310000': [['#'], ['#']]}
{'320000': ['$', ['1']]}
{'321000': [['abc', 'abc']]}
{'322000': [['#'], ['#']]}
{'400000': [['yeah', 'baby']]}
perhaps a nested list can implement this or I need a tree type?
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': ['$', ['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:
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': ['$', ['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.
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': (['$', ['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.
精彩评论