[portland] Need Help With a For Loop

kirby urner kirby.urner at gmail.com
Fri Mar 21 20:19:04 CET 2008


OK, so here's my solution, using only basic Python:

varData = [
    ('Low', 'Variety', 'Fish', 'Wildlife', 'Decay S-Curve',
     1, 0.0, 100.0, 0.0, 50.0, 0.0, 50.0, 50.0, 50.0, 100.0, 1.0, 2),
    ('High', 'Variety', 'Fish', 'Wildlife', 'Growth S-Curve',
     2, 0.0, 100.0, 0.0, 50.0, 0.0, 100.0, 50.0, 50.0, 100.0, 1.0, 2),
    ('Low', 'SpecialConcern', 'Fish', 'Wildlife', 'Decay S-Curve',
     1, 0.0, 50.0, 0.0, 50.0, 0.0, 50.0, 50.0, 0.0, 50.0, 1.0, 3),
    ('Moderate', 'SpecialConcern', 'Fish', 'Wildlife', 'Bell Curve',
     2, 0.0, 100.0, 0.0, 100.0, 0.0, 50.0, 50.0, 50.0, 50.0, 1.0, 3),
    ('Many', 'SpecialConcern', 'Fish', 'Wildlife', 'Growth S-Curve',
     3, 50.0, 100.0, 50.0, 50.0, 0.0, 100.0, 50.0, 100.0, 50.0, 1.0, 3)]

def testfunction(thelist):

    def header():
        print "Starting a new plot..."
        print "Component: %s  Subcomponent: %s  Parent: %s" \
                % (component, subcomponent, parent)

    def footer():
        print "Ending this plot\n"

    ranked = sorted(thelist,key=lambda x: x[3])
    ranked = sorted(thelist,key=lambda x: x[2])
    ranked = sorted(thelist,key=lambda x: x[1])

    starting = True

    for row in ranked:

        if starting:
            component = row[3]
            subcomponent = row[2]
            parent = row[1]
            header()
            starting = False

        if (component == row[3]
            and subcomponent == row[2]
            and parent == row[1]):

            print "\t%s" % row[4]

        else:

            footer()

            component = row[3]
            subcomponent = row[2]
            parent = row[1]

            header()
            print "\t%s" % row[4]


    footer()

def test():
    print varData
    print "---------"
    testfunction(varData)

=========
Interactively:

>>> reload(richproject)
<module 'richproject' from 'C:\Python25\lib\site-packages\richproject.py'>
>>> richproject.test()
[('Low', 'Variety', 'Fish', 'Wildlife', 'Decay S-Curve', 1, 0.0,
100.0, 0.0, 50.0, 0.0, 50.0, 50.0, 50.0, 100.0, 1.0, 2), ('High',
'Variety', 'Fish', 'Wildlife', 'Growth S-Curve', 2, 0.0, 100.0, 0.0,
50.0, 0.0, 100.0, 50.0, 50.0, 100.0, 1.0, 2), ('Low',
'SpecialConcern', 'Fish', 'Wildlife', 'Decay S-Curve', 1, 0.0, 50.0,
0.0, 50.0, 0.0, 50.0, 50.0, 0.0, 50.0, 1.0, 3), ('Moderate',
'SpecialConcern', 'Fish', 'Wildlife', 'Bell Curve', 2, 0.0, 100.0,
0.0, 100.0, 0.0, 50.0, 50.0, 50.0, 50.0, 1.0, 3), ('Many',
'SpecialConcern', 'Fish', 'Wildlife', 'Growth S-Curve', 3, 50.0,
100.0, 50.0, 50.0, 0.0, 100.0, 50.0, 100.0, 50.0, 1.0, 3)]
---------
Starting a new plot...
Component: Wildlife  Subcomponent: Fish  Parent: SpecialConcern
	Decay S-Curve
	Bell Curve
	Growth S-Curve
Ending this plot

Starting a new plot...
Component: Wildlife  Subcomponent: Fish  Parent: Variety
	Decay S-Curve
	Growth S-Curve
Ending this plot


On Fri, Mar 21, 2008 at 10:53 AM, kirby urner <kirby.urner at gmail.com> wrote:
> OK Rich, so just to be sure:
>
>  This is the input:
>
>
>  varData = [("Low","Variety","Fish","Wildlife","Decay
>  S-Curve",1,0.0,100.0,0.0,50.0,0.0,50.0,50.0,50.0,100.0,1.0,2),
>  ("High","Variety","Fish","Wildlife","Growth
>  S-Curve",2,0.0,100.0,0.0,50.0,0.0,100.0,50.0,50.0,100.0,1.0,2),
>  ("Low","SpecialConcern","Fish","Wildlife","Decay
>  S-Curve",1,0.0,50.0,0.0,50.0,0.0,50.0,50.0,0.0,50.0,1.0,3),
>  ("Moderate","SpecialConcern","Fish","Wildlife","Bell
>  Curve",2,0.0,100.0,0.0,100.0,0.0,50.0,50.0,50.0,50.0,1.0,3),
>  ("Many","SpecialConcern","Fish","Wildlife","Growth
>  S-Curve",3,50.0,100.0,50.0,50.0,0.0,100.0,50.0,100.0,50.0,1.0,3)]
>
>  And this is the desired output:
>
>
>  Starting a new plot ...
>  Component:  Wildlife Subcomponent:  Fish Parent:  Variety
>        Decay S-Curve
>        Growth S-Curve
>  Ending this plot ...
>
>  Starting a new plot ...
>  Component:  Wildlife Subcomponent:  Fish Parent:  SpecialConcern
>        Decay S-Curve
>        Bell Curve
>        Growth S-Curve
>  Ending this plot ...
>
>  > I hope this makes it easy for you!
>
>  I think so.  Back to you shortly.
>
>  Thanks for helping out, good team work.
>
>  Kirby
>


More information about the Portland mailing list