[Tutor] sorting into lists

Tim Wilson wilson@visi.com
Sat, 21 Sep 2002 00:44:34 -0500


On Fri, Sep 20, 2002 at 10:07:37PM -0700, Danny Yoo wrote:
> 
> Hmmm... Let's assume, for the moment, that our list of events has a method
> named "date()".  (If they don't, perhaps we can add it in as a method.)
> Then we can adjust the dictionary approach we used earlier so that it
> groups the events by date --- we can use the date as keys into the
> dictionary:
> 
> ###
> def formGroups(events):
>     groups = {}
>     for event in events:
>         groups.setdefault(event.date(), []).append(event)
>     return groups.values()
> ###

OK, this function works very well! I knew there was a
nice and clean solution. You can see a proof of concept at
http://isd197.org/sibley/displayEvents if you're interested. This page
is generated on the fly from the SQL database and will rotate to show
about a week's worth of events.

> We can apply this generalization if we modify formGroups() to take in a
> definition of sameness as a second parameter!

Very cool! This is one to tuck away for future reference.

-Tim

-- 
Tim Wilson      |   Visit Sibley online:   | Check out:
Henry Sibley HS |  http://www.isd197.org   | http://www.zope.com
W. St. Paul, MN |                          | http://slashdot.org
wilson@visi.com |  <dtml-var pithy_quote>  | http://linux.com