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
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
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
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
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
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)
>
>
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
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
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
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:
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
11 matches
Mail list logo