[portland] Need Help With a For Loop
kirby urner
kirby.urner at gmail.com
Fri Mar 21 20:52:50 CET 2008
OK, sorry, I was messing up: here's the real answer.
If your SQL is able to retrieve the rows in the needed
order (which SQL is good at) then you don't need
these lines of code:
def primarykey(x):
return (x[3],x[2],x[1],x[5])
ranked = sorted(thelist,key=primarykey)
Keep everything else the same and I think it'll work?
Kirby
>>> 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)]
---------
('Wildlife', 'Fish', 'Variety', 1)
('Wildlife', 'Fish', 'Variety', 2)
('Wildlife', 'Fish', 'SpecialConcern', 1)
('Wildlife', 'Fish', 'SpecialConcern', 2)
('Wildlife', 'Fish', 'SpecialConcern', 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
=============== richproject.py =================
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"
def primarykey(x):
return (x[3],x[2],x[1],x[5])
ranked = sorted(thelist,key=primarykey)
for i in thelist:
print (i[3],i[2],i[1],i[5])
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)
On Fri, Mar 21, 2008 at 12:45 PM, kirby urner <kirby.urner at gmail.com> wrote:
> On Fri, Mar 21, 2008 at 12:40 PM, kirby urner <kirby.urner at gmail.com> wrote:
> > Hi Rich,
> >
> > I didn't go as far as plot sequence number, but that could be added
> > with one more line of code inserted after the existing lines:
> >
> > ranked = sorted(ranked, key=little lambda with the right key number).
> >
>
> Now wait, this is wrong...
>
> I'm screwing up with this sorting thing. Hold on...
>
> Kirby
>
More information about the Portland
mailing list