[portland] Iterating Through Tuples In A List

kirby urner kirby.urner at gmail.com
Fri Feb 15 23:54:11 CET 2008


On Feb 15, 2008 12:20 PM, Rich Shepard <rshepard at appl-ecosys.com> wrote:
>    I've not yet figured out the code that gives me what I need in this one
> method. I'm sure it's simple, but the proper approach eludes me.
>
>    Data start with a list of tuples. I iterate through the list and extract
> each tuple using,
>
> for i in range(len(self.appData.tsets)):
>        self.row = self.appData.tsets[i]
>

Remember that you can go:

for row in self.appData.tsets:
    print row

i.e. it's not necessary to take the len
of something simply to range through
it by subscript.  Way less subscripting
goes on in Python as a result.

> I've not succeeded in writing the code to do the above. If I print the
> components and the total number of subcomponents, for example, I get output
> like this:
>
> HabitatComplexity       2
> HabitatComplexity       2
> SpecialConcern  3
> SpecialConcern  3
> SpecialConcern  3
> Variety         2
> Variety         2
>
> If I print the compnent, total of subcomponents, subcomponent sequential
> number, and subcomponent curve shape I get output like this:
>
> HabitatComplexity       2       1       Decay S-Curve
> HabitatComplexity       2       2       Growth S-Curve
> SpecialConcern  3       1       Decay S-Curve
> SpecialConcern  3       2       Bell Curve
> SpecialConcern  3       3       Growth S-Curve
> Variety         2       1       Decay S-Curve
> Variety         2       2       Growth S-Curve
>

These look mutually consistent to me.  Do you have input into what
data structure to use?  A dictionary might be better, e.g.

{"HabitatComplexity":("Decay S-Curve", "Growth S-Curve"),
 "SpecialConcern":("Decay S-Curve", "Bell Curve", "Growth S-Curve"),
 "Variety":("Decay S-Curve", "Growth S-Curve")}

Nevermind though.  Sounds like you've got something more like:

[("HabitatComplexity" 2, ("Decay S-Curve", "Growth S-Curve")),
 ("SpecialConcern", 3, ("Decay S-Curve", "Bell Curve", "Growth S-Curve")),
 ("Variety", 2, ("Decay S-Curve", "Growth S-Curve"))]

Number of subcomponents is sort of "data sugar" as it'd be easy
to just get it from len, but oh well.

>    I need to call the plotting library for each component, for the maximum
> number of subcomponents, so that all subcomponents are plotted on the same
> set of axes. This is what I've not yet figured out how to do.

You could use your "Decay S-Curve" type strings (are they strings?)
as pointers to plotting functions?

Using the above data structure (list of tuples):

>>> mydata = [("HabitatComplexity", 2, ("Decay S-Curve", "Growth S-Curve")),
 ("SpecialConcern", 3, ("Decay S-Curve", "Bell Curve", "Growth S-Curve")),
 ("Variety", 2, ("Decay S-Curve", "Growth S-Curve"))]

>>> def f(x):  return "Decay S-Curve"  # pretend plot function

>>> def g(x):  return "Growth S-Curve" # pretend plot function

>>> def b(x):  return "Bell Curve" # pretend plot function

>>> plots = {"Decay S-Curve": f, "Growth S-Curve": g, "Bell Curve": b}
 # pointers to functions

>>> for row in mydata:
	subcomps = row[2]
	print row[0]
	for curve in subcomps:
		print "plotting: ",
		print plots[curve](1)  # <--- (1) is "dummy data" -- the functions take arg x.

		
HabitatComplexity
plotting:  Decay S-Curve
plotting:  Growth S-Curve
SpecialConcern
plotting:  Decay S-Curve
plotting:  Bell Curve
plotting:  Growth S-Curve
Variety
plotting:  Decay S-Curve
plotting:  Growth S-Curve

>
> Suggestions appreciated,
>
> Rich
>

Stay away from subscripts if at all possible.  They're not as
necessary in Python as in some other languages.

Also, based on previous postings, I'm worried you don't
really need "self" -- you're inside some class definition here?

Kirby

> --
> 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
> _______________________________________________
> Portland mailing list
> Portland at python.org
> http://mail.python.org/mailman/listinfo/portland
>


More information about the Portland mailing list