Struggling with basics

George Sakkis gsakkis at rutgers.edu
Sun Sep 25 20:43:19 EDT 2005


"Jason" <jason at jasonmhirst.co.uk> wrote:

> What I'd like to know is do you think it would be better to sort the
> list in memory, or print it out sorted?  If the latter, then naturally
> I'd need to change the showScores section to show the list in a reverse
> order.  But, would sorting the list in memory be more effective?

The list *is* sorted; the thing is that it is in ascending order (from lowest to highest) but you
would rather have it in descending. There are (at least) two alternatives:

1. Keep the list as it is now in ascending order and print it in reverse. In python 2.4, this is as
elegant and efficient as it can, using the reversed() builtin function. Just replace in showScores
"for score,name in self.hiScores" with "for score,name in reversed(self.hiScores)". reversed()
returns an iterator over the sequence, not a new list, so the memory overhead is minimal.

2. Instead of storing (score,name) pairs, store (-score,name). When a list of the latter is in
ascending order, the former is in descending. In this case of course, you have to make sure that
showScores() and lastScore() return the actual (positive) score, not the stored (negative) one.

I would go for the first alternative but YMMV.

George





More information about the Python-list mailing list