[Tutor] Lists on the fly?

Carlos carloslara at web.de
Sat Dec 23 11:17:55 CET 2006


Hi,

First of all, thanks for your help.

This is how this ended up:

Elements =  [
                            {'Name': 'Access', 'Parent': 'Plot', 
'Level': 1.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0},
                            {'Name': 'Circulation_01', 'Parent': 
'Access', 'Level': 1.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0},
                            {'Name': 'Circulation_02', 'Parent': 
'Access', 'Level': 2.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0},
                            {'Name': 'Circulation_03', 'Parent': 
'Access', 'Level': 3.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0},
                            {'Name': 'Int_Circ_01', 'Parent': 
'Circulation_01', 'Level': 1.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0},
                            {'Name': 'Int_Circ_02', 'Parent': 
'Circulation_01', 'Level': 1.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0},
                            {'Name': 'Int_Circ_03', 'Parent': 
'Circulation_02', 'Level': 2.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0},
                            {'Name': 'Int_Circ_04', 'Parent': 
'Circulation_02', 'Level': 2.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0},
                            {'Name': 'Int_Circ_05', 'Parent': 
'Circulation_03', 'Level': 3.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0},
                            {'Name': 'Int_Circ_06', 'Parent': 
'Circulation_03', 'Level': 3.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0}
                       ]

# Get Levels List 
    for Element in Elements:
        Lev_List['Level_%i' % (Element['Level'])] = []

    # Append Element to corresponding Level
    for Element in Elements:
        Lev_List['Level_%i' % (Element['Level'])].append(Element['Name'])

    print Lev_List

And this is the result:

Lev_List = {
                     'Level_1': ['Access', 'Circulation_01', 
'Int_Circ_01', 'Int_Circ_02'],
                     'Level_2': ['Circulation_02', 'Int_Circ_03', 
'Int_Circ_04'],
                     'Level_3': ['Circulation_03', 'Int_Circ_05', 
'Int_Circ_06']
                    }

It works fine, as far as I can tell. I mean, no matter how many 'Levels' 
it will allways get them right (levels so far are only integers). 
Probably the first loop is a little redundant, but performance is not 
one of my concerns now, but in the future the project will not be 
composed by a handful of spaces, probably by hundreds or more.

Regards,
Carlos








More information about the Tutor mailing list