Tuple question

Dan Christensen jdc at uwo.ca
Fri Sep 3 03:39:25 CEST 2004


I'm not sure I buy the arguments against an index operation for
tuples.  For example, suppose we were conducting a vote about
something, hmm, let's say decorator syntax.  <wink>  And suppose that
each person is allowed three distinct votes, ranked in order, with the
first vote getting 3 points, the second 2, and the third 1.  We might
store the votes in a database, whose rows would naturally be tuples
like

  ('J2', 'C64', 'X11')

Now suppose we want to calculate the total number of points for
proposal X, but don't need to compute the totals for the other
choices.  Code like the following would be a pretty natural
approach:

    for row in rows:
        try:
            points += 3-row.index(X)
        except:
            pass

I realize that there are different ways to code it, but most
are simply reimplementations of the proposed index function.

Dan



More information about the Python-list mailing list