[portland] Need Help With a For Loop
jason kirtland
jek at discorporate.us
Fri Mar 21 20:58:06 CET 2008
And here's one for Python 2.5:
def testfunction(rows):
"""Plots a sequence of data rows.
[(impact, parent, subcategory, category, plot type,
sequence, data...), ... ]
"""
from itertools import groupby
from operator import itemgetter
ranked = sorted(rows, key=itemgetter(3, 2, 1, 5))
grouping_iterator = groupby(ranked, key=itemgetter(3, 2, 1))
for (component, subcomponent, parent), grouped in grouping_iterator:
print "Starting a new plot .."
print "Component: %s Subcomponent: %s Parent: %s" % (
component, subcomponent, parent)
for row in grouped:
plot_type, data = row[4], row[6:]
print "\t%s" % plot_type
print "Ending this plot ..."
testfunction(varData)
kirby urner wrote:
> 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
>>
> _______________________________________________
> Portland mailing list
> Portland at python.org
> http://mail.python.org/mailman/listinfo/portland
>
More information about the Portland
mailing list