Re: [Tutor] sorting objects on two attributes

2008-03-04 Thread Kent Johnson
Here is a version based on Andreas' solution using groupby() that avoids the decorate-undecorate by using the key= parameter for the second sort: l.sort(key=lambda x: (x.content_type, x.submit_date)) t = [ list(items) for key, items in itertools.groupby(l, key=lambda x: (x.content_type)) ] t.sort

Re: [Tutor] sorting objects on two attributes

2008-03-03 Thread Eric Abrahamsen
On Mar 4, 2008, at 11:04 AM, Kent Johnson wrote: > Eric Abrahamsen wrote: >> Itertools.groupby is totally impenetrable to me > > Maybe this will help: > http://personalpages.tds.net/~kent37/blog/arch_m1_2005_12.html#e69 > > Kent > It did! Thanks very much. I think I understand now what's going o

Re: [Tutor] sorting objects on two attributes

2008-03-03 Thread Kent Johnson
Eric Abrahamsen wrote: > Itertools.groupby is totally impenetrable to me Maybe this will help: http://personalpages.tds.net/~kent37/blog/arch_m1_2005_12.html#e69 Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] sorting objects on two attributes

2008-03-03 Thread Eric Abrahamsen
Well I expected to learn a thing or two, but I didn't expect not to understand the suggestions at all! :) Thanks to everyone who responded, and sorry for the typo (it was meant to be object_id throughout, not content_type). So far Michael's solution works and is most comprehensible to me – i

[Tutor] sorting objects on two attributes

2008-03-03 Thread Michael H. Goldwasser
Hello Eric, Your basic outlook is fine, but you can do it much more efficiently with a single sort. Here's the way I'd approach the task (untested): # -- # first compute the latest date for each id group; uses O(n) time newest = {} f

Re: [Tutor] sorting objects on two attributes

2008-03-03 Thread Kent Johnson
Chris Fuller wrote: > You could have a hierarchical sort > function: > > def hiersort(a,b): >if a.attr1 != b.attr1: > return cmp(a.attr1, b.attr1) >else: > if a.attr2 != b.attr2: > return cmp(a.attr2, b.attr2) > else: > return cmp(a.attr3, b.att3) > >

Re: [Tutor] sorting objects on two attributes

2008-03-03 Thread Chris Fuller
Almost. And better than my original idea. You could have a hierarchical sort function: def hiersort(a,b): if a.attr1 != b.attr1: return cmp(a.attr1, b.attr1) else: if a.attr2 != b.attr2: return cmp(a.attr2, b.attr2) else: return cmp(a.attr3, b.att3) l

Re: [Tutor] sorting objects on two attributes

2008-03-03 Thread Kent Johnson
Andreas Kostyrka wrote: > l.sort(key=lambda x: (x.content_type, x.submit_date)) > > Now, you can construct a sorted list "t": > > t = [] > for key, item_iterator in itertools.groupby(l, key=lambda x: (x.content_type, > x.submit_date)): > sorted_part = sorted(item_iterator, key=lambda x: x.s

Re: [Tutor] sorting objects on two attributes

2008-03-03 Thread Kent Johnson
Eric Abrahamsen wrote: > I have a list of objects, each of which has two attributes, object_id > and submit_date. What I want is to sort them by content_type, then by > submit_date within content_type, and then sort each content_type block > according to which block has the newest object by

Re: [Tutor] sorting objects on two attributes

2008-03-03 Thread Andreas Kostyrka
Well, this assumes that all named attributes do exist. If not, you need to replace x.attr with getattr(x, "attr", defaultvalue) ;) l.sort(key=lambda x: (x.content_type, x.submit_date)) Now, you can construct a sorted list "t": t = [] for key, item_iterator in itertools.groupby(l, key=lambda x:

[Tutor] sorting objects on two attributes

2008-03-03 Thread Eric Abrahamsen
I have a grisly little sorting problem to which I've hacked together a solution, but I'm hoping someone here might have a better suggestion. I have a list of objects, each of which has two attributes, object_id and submit_date. What I want is to sort them by content_type, then by submit_date