[portland] Dictionary As Switch Statement

kirby urner kirby.urner at gmail.com
Fri Jan 18 01:48:24 CET 2008


>
>   What have I done incorrectly that I cannot produce the value for a given
> key? When I pass the value of the key (it's the fifth item in a tuple so
> it's addressed as 'row[4]') all I get is 'None.'
>
> Rich
>

Hey Rich --

Dictionaries very much don't care about order, i.e. those keys
are defined *not* to come in first, second, third place etc.
plus forget about 'alphabetical'.  Imagine curvePlot.keys()
listing the keys in a different order each time, and realize
why in Python we're happy with that outcome.

The only way to grab a single value that's not idiosyncratic is
to reach in with a key, a string in your example e.g.:

curvePlot[ 'Decay S-Curve' ]

should return zCurve(self) but then I'm suspicious of why you
have "self" mentioned, as that's normally not passed as an
argument.  Are your functions or objects returning None then?

Note:

curvePlot[0] is by definition meaningless (if you want sequential
access, don't use a dictionary, use a list or tuple).

Kirby


On Jan 17, 2008 4:16 PM, Rich Shepard <rshepard at appl-ecosys.com> wrote:
>   I want to use a dictionary to select the appropriate function to call
> based on the key value. However, I'm missing something simple here as I try
> to implement it.
>
>   The dictionary:
>
>     curvePlot = {
>       'Decay S-Curve'     : zCurve(self),
>       'Bell Curve'        : bellCurve(self),
>       'Growth S-Curve'    : sCurve(self),
>       'Beta'              : betaCurve(self),
>       'Data'              : dataCurve(self),
>       'Linear Increasing' : linIncrCurve(self),
>       'Linear Decreasing' : linDecrCurve(self),
>       'Left Shoulder'     : leftShouldCurve(self),
>       'Trapezoid'         : trapCurve(self),
>       'Right Shoulder'    : rightShouldCurve(self),
>       'Triangle'          : triangCurve(self),
>       'Singleton'         : singleCurve(self),
>       'Rectangle'         : rectCurve(self),
>       'Outcome'           : resultsCurve(self)
>       }
>
>   I can print out the keys, in a different order; 'print curvePlot.keys()'
> yields:
>
> ['Left Shoulder', 'Singleton', 'Outcome', 'Bell Curve', 'Right
> Shoulder', 'Growth S-Curve', 'Beta', 'Linear Increasing', 'Decay S-Curve',
> 'Linear Decreasing', 'Trapezoid', 'Triangle', 'Data', 'Rectangle']
>
>   But, when I try to print the entire dictionary, the values are not shown;
> 'print curvePlot' produces:
>
> {'Left Shoulder': None, 'Singleton': None, 'Outcome': None, 'Bell Curve':
> None, 'Right Shoulder': None, 'Growth S-Curve': None, 'Beta': None, 'Linear
> Increasing': None, 'Decay S-Curve': None, 'Linear Decreasing': None,
> 'Trapezoid': None, 'Triangle': None, 'Data': None, 'Rectangle': None}
> --
> 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