Tuple question
Jason Lai
jmlai at uci.edu
Thu Sep 2 23:53:06 EDT 2004
Dan Christensen wrote:
> Jason Lai <jmlai at uci.edu> writes:
>
>
>>Dan Christensen wrote:
>>
>>
>>> 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
>>
>>for index, row in enumerate(rows):
>> points += 3 - index
>
>
> [I changed "i" to "index" in the last line.]
>
> That doesn't do the same thing, but that's because my description
> wasn't clear. rows is a list of tuples, with each tuple being one
> person's three votes. E.g.
>
> rows = [('J2', 'C64', 'X11'), ('U2', 'J2', 'P3000')]
>
> So if X == 'J2', the code should calculate 3+2=5 points.
>
> Dan
Ah, okay, I see my mistake. I should read closer before replying :P
Well, I see your point, although I still don't think it would happen
that often. I also don't see it as a newbie trap, because currently all
the other "mutable sequence" member functions are only for lists, not
tuples. Index and count are the only mutable sequence functions that
might apply to tuples. One could argue that those functions might be
useful (to a lesser extent, probably) for iterators too.
- Jason Lai
More information about the Python-list
mailing list