Tuple question
Jason Lai
jmlai at uci.edu
Thu Sep 2 22:23:13 EDT 2004
Dan Christensen wrote:
> 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
Well,
for index, row in enumerate(rows):
points += 3 - i
It's not really reimplementing the index function. You already have the
index. I think for most of the cases where people use tuples, you
already know what you're using each index for.
- Jason
More information about the Python-list
mailing list