473,396 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Using class-attribute as key to sort?

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 Customer(object):
def __init__(self, idnumber, review):
self.idnumber = idnumber
self.reviews = [review]

def addReview(self, review):
self.reviews.append(review)

def averageGrade(self):
tot = 0
for review in self.reviews:
tot += review.grade
return tot / len(self.reviews)

#general sort that takes list+spec of objectkey to sort on?
def sortReviews(self):
def qsort(lista):
if len(lista) != 0:
return qsort([x for x in lista[1:] if x.grade <
lista[0].grade]) +[lista[0]] + \
qsort([x for x in lista[1:] if x.grade >=
lista[0].grade])
else:
return []
return qsort(self.reviews)

def medianGrade(self):
length = len(self.reviews)
sr = self.sortReviews()
#sr = self.reviews.sort(key=self.reviews.instance.grade)
if length % 2 == 0:
return (sr[int(length/2)].grade + sr[int(length/
2-1)].grade) / 2
else:
if length == 1:
return sr[0].grade
else:
return sr[int(round(length/2)-1)].grade
Aug 29 '08 #1
3 934
cnb:
In median grade, can I sort on Review-grade somehow?
operator.attrgetter may help.

Bye,
bearophile
Aug 29 '08 #2
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
========
Follow the path of the Iguana...
http://rebertia.com

On Fri, Aug 29, 2008 at 11:22 AM, cnb <ci**********@yahoo.sewrote:
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 Customer(object):
def __init__(self, idnumber, review):
self.idnumber = idnumber
self.reviews = [review]

def addReview(self, review):
self.reviews.append(review)

def averageGrade(self):
tot = 0
for review in self.reviews:
tot += review.grade
return tot / len(self.reviews)

#general sort that takes list+spec of objectkey to sort on?
def sortReviews(self):
def qsort(lista):
if len(lista) != 0:
return qsort([x for x in lista[1:] if x.grade <
lista[0].grade]) +[lista[0]] + \
qsort([x for x in lista[1:] if x.grade >=
lista[0].grade])
else:
return []
return qsort(self.reviews)

def medianGrade(self):
length = len(self.reviews)
sr = self.sortReviews()
#sr = self.reviews.sort(key=self.reviews.instance.grade)
if length % 2 == 0:
return (sr[int(length/2)].grade + sr[int(length/
2-1)].grade) / 2
else:
if length == 1:
return sr[0].grade
else:
return sr[int(round(length/2)-1)].grade
--
http://mail.python.org/mailman/listinfo/python-list
Aug 29 '08 #3


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).

Aug 29 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Christopher Benson-Manica | last post by:
Why can't I use a class destructor in a using declaration: using MyClass::~MyClass; ? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org ...
8
by: acb | last post by:
Hi, I wrote a DLL Component (using Visual Studio 2005) and managed to include it into a C# Console application. I am now trying to include this component into a Web project. I copy the DLL...
1
by: Peter Nofelt | last post by:
Hey All, I am having issue with serializing a class and its base class using ..net 2.0. I am using IXmlSerializable I've provided code displaying my at the bottom of this post. Thanks ahead...
1
by: Peter Nofelt | last post by:
Hey All, I am having issue with serializing a class and its base class using ..net 2.0. I am using IXmlSerializable I've provided code displaying my at the bottom of this post. Thanks ahead...
8
by: =?Utf-8?B?Q2hyaXMgSGFsY3Jvdw==?= | last post by:
Hi there I've successfully added some .NET validation controls to a page (using <asp:RequiredFieldValidator ...), however when I try to set the 'display' property to 'dynamic', my page then...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.