[portland] Need Help With a For Loop

Rich Shepard rshepard at appl-ecosys.com
Thu Mar 20 21:24:59 CET 2008


On Thu, 20 Mar 2008, Dylan Reinhardt wrote:

> In most cases, I would strongly recommend ditching this idiom:
>
> for i in range(len(collection)):
>    doSomething(collection[i])
>
> In favor of this one:
>
> for item in collection:
>    doSomething(item)

Dylan,

   Tried this before but still could not get it correct. Just tried again,
too. Here is a set of three tuples for the same parent variable. I want to
loop through the three plotting the appropriate curve each time:

[("Few","Abundance","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,2),
("Moderate","Abundance","Fish","Wildlife","Bell
pCurve",2,0.0,100.0,0.0,100.0,0.0,50.0,50.0,50.0,100.0,1.0,2),
("Many","Abundance","Fish","Wildlife","Growth
S-Curve",3,50.0,100.0,50.0,50.0,0.0,50.0,50.0,100.0,50.0,1.0,2)]

   The parent is "Abundance," the sequence number is item[4], the total
number of curves to plot on the same set of axes is item[16], and item[4] is
used to call the appropriate plotting function.

   Starting like this does not work:

 	for item in compList:
           pylab.hold(True)
           if compList[0][4] == 'Decay S-Curve':
             testFunctions.zCurve(compList[0][10],compList[0][9])
           elif compList[0][4] == 'Bell Curve':
             testFunctions.gaussCurve(compList[0][14],compList[0][14])
           elif compList[0][4] == 'Growth S-Curve':
             testFunctions.sCurve(compList[0][8],compList[0][11])
  	  ...

   I can't do 'for item[0][1] in compList:' because that generates
"NameError: global name 'item' is not defined".

Thanks,

Rich

-- 
Richard B. Shepard, Ph.D.               |  Integrity            Credibility
Applied Ecosystem Services, Inc.        |            Innovation
<http://www.appl-ecosys.com>     Voice: 503-667-4517      Fax: 503-667-8863


More information about the Portland mailing list