Using class-attribute as key to sort?

2008-08-29 Thread cnb
In median grade, can I sort on Review-grade somehow? something like: sr = self.reviews.sort(key=self.reviews.instance.grade) class Review(object): def __init__(self, movieId, grade, date): self.movieId = movieId self.grade = grade self.date = date class

Re: Using class-attribute as key to sort?

2008-08-29 Thread bearophileHUGS
cnb: In median grade, can I sort on Review-grade somehow? operator.attrgetter may help. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Using class-attribute as key to sort?

2008-08-29 Thread Chris Rebert
Just use the key argument to list.sort; e.g. class Foo(object): def __init__(self, x): self.x = x def __repr__(self): return Foo:+str(self.x) foos = [Foo(75), Foo(10), Foo(-1)] foos.sort(key = lambda foo: foo.x) print foos #= [Foo:-1, Foo:10, Foo:75] - Chris

Re: Using class-attribute as key to sort?

2008-08-29 Thread Terry Reedy
cnb wrote: In median grade, can I sort on Review-grade somehow? Google Python sort key attribute the first hit (after your post) is http://wiki.python.org/moin/HowTo/Sorting Find 'attribute' on that page for your answer (two options). -- http://mail.python.org/mailman/listinfo/python-list