Friday, 15 March 2013

dictionary - How can I dynamically create a tree using Python dict -


Before I do this, without knowing the exact number of levels and nodes, I am going to create a tree by using Python Dion .

I work in the following things: size, color, weight = item .get_attr () if the item is not in item_data: item_data [size] = {} if the item is not color in the item_data [size]: item_data [size] [color ] = {} If the weight is not in item_data [size] [color]: item_data [size] [color] [weight] = [] # tree ...

Like a duck mill Or

  item_data == {'L': {'red': {45: [. ..], 50: [...]}, 'green': {40: [...]}}, 'XL': {...}}   

But this is not flexible, for example if I want to add another attribute to 'value'? I have to know this and if I have to add another in the above code.

I am thinking of doing it in the following manner, but it does not know how to do it in some lines for etr in etter

  attrs = [item .get_attr ()]: # Create a tree here ..   

Thanks advance!

Here's what's relevant. Otherwise, here is a basic version extending your idea of ​​using attr loop (code not tested). For items in item < / Div>

No comments:

Post a Comment