[Tutor] sorting into lists

Tim Wilson wilson@visi.com
Fri, 20 Sep 2002 23:32:41 -0500


On Fri, Sep 20, 2002 at 09:22:51PM -0700, Danny Yoo wrote:
> 
> On Fri, 20 Sep 2002, Tim Wilson wrote:
> 
> > Hey everyone,
> >
> > Let's say I've got a list like the following:
> >
> > l = [1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4]
> >
> > I'm trying to figure out a good way to turn this list into
> >
> > [[1, 1, 1], [2, 2], [3, 3, 3, 3], [4, 4]]
> 
> Hi Tim,
> 
> Is this grouping identical elements?  Do we have to worry about what
> happens when the list isn't in order?  More information on the problem
> might allow us to write more appropriate programs.

Danny,

Thanks for the quick response. You were right to suspect that my real
problem is not quite as straight-forward as the example I provided.
Actually, I've got a list of objects that are returned from a SQL query
in Zope. Each of those objects contains a DateTime field and is already
sorted on that field by the SQL query. (In other words, I can assume
that the original list is sorted.)

The query results correspond to events in a larger database of events.
My goal is to produce an "Upcoming events" table on my Web site with the
events grouped by day. For example, "Today," "Tomorrow," "Mon. 9/23,"
etc. The reason I need the list of lists is because it's the most
obvious way I can think of to iterate over the events and print the date
at the beginning of each inner loop. (Think nested for loops.)

Does that help or are you sorry you asked? :-)

-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