itertools.groupby

Steve Howell showell30 at yahoo.com
Tue May 29 18:18:25 EDT 2007


--- Carsten Haese <carsten at uniqsys.com> wrote:
> As an aside, while groupby() will indeed often be
> used in conjunction
> with sorted(), there is a significant class of use
> cases where that's
> not the case: I use groupby to produce grouped
> reports from the results
> of an SQL query. In such cases, I use ORDER BY to
> guarantee that the
> results are supplied in the correct order rather
> than using sorted().
> 

Although I'm not trying to preoptimize here, it seems
a mistake to use sorted() and groupby() in
conjunction, if you're dealing with a use case where
you don't need the groups themselves to be sorted. 
Instead, you'd use something more straightforward (and
faster, I think) like the cookbook "SQL-like Group By"
example.  

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/259173

It seems to me that the classic use case for
itertools.groupby() is the never-ending stream of data
where you're just trying to pick out consecutive
related elements.  For example, if you're snooping on
syslog, you could use groupby() to avoid repeating
duplicate messages to some other output stream.




       
____________________________________________________________________________________Take the Internet to Go: Yahoo!Go puts the Internet in your pocket: mail, news, photos & more. 
http://mobile.yahoo.com/go?refer=1GNXIC



More information about the Python-list mailing list