[portland] Need Help With a For Loop
kirby urner
kirby.urner at gmail.com
Fri Mar 21 21:07:19 CET 2008
Arrgh, it was the *debugging* code that was borked, my
sorting solution was correct after all.
def primarykey(x):
return (x[3],x[2],x[1],x[5])
ranked = sorted(thelist,key=primarykey)
for i in ranked: # was iterating through presorted list before
print (i[3],i[2],i[1],i[4],i[5])
Anyway, Jason's 2.5 solution is cooler.
OK, gotta run...
Kirby
from random import shuffle
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):
shuffle(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 ranked:
print (i[3],i[2],i[1],i[4],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)
---------
('Wildlife', 'Fish', 'SpecialConcern', 'Decay S-Curve', 1)
('Wildlife', 'Fish', 'SpecialConcern', 'Bell Curve', 2)
('Wildlife', 'Fish', 'SpecialConcern', 'Growth S-Curve', 3)
('Wildlife', 'Fish', 'Variety', 'Decay S-Curve', 1)
('Wildlife', 'Fish', 'Variety', 'Growth S-Curve', 2)
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 1:01 PM, kirby urner <kirby.urner at gmail.com> wrote:
> Shoot, I'm still doing something wrong. Why don't my primary
> key tuples sort the way I think they should??
>
> Damn.
>
> Gotta run an errand, then I'm gonna solve this!
>
> Kirby
>
>
>
>
> On Fri, Mar 21, 2008 at 12:52 PM, kirby urner <kirby.urner at gmail.com> wrote:
> > 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
>
More information about the Portland
mailing list