How to sort by a particular attribute of a class?
I have a model with an attribute which keeps track of the price. Right now, I have a list of that certain model. Is there anyway to rearrange the list to sort by that particular attribute? Is python smart enough to know that the attribute is a value which can be sorted? I am not keeping keeping track of the instances of a particular model using a database (it is not needed for what I am doing, so I cannot just retrieve the instances from th开发者_如何学JAVAe database in sorted order) Thanks!
You can use the inbuilt sorted
function, together with a custom-made function that returns the price of an object:
class Dummy(object) :
pass
def getPrice(obj) :
return obj.price
d0 = Dummy()
d0.price = 56.
d1 = Dummy()
d1.price=16.
d2 = Dummy()
d2.price=786.
d3 = Dummy()
d3.price=5.5
elements = [d0, d1, d2, d3]
print 'Pre-sorting:'
for elem in elements :
print elem.price
sortedElements = sorted(elements, key=getPrice)
print 'Post-sorting:'
for elem in sortedElements :
print elem.price
This would also work via any method of your class that returns the price, e.g.
class Dummy(object) :
def __init__(self, price) :
self._price = price
def getPrice(self) :
return self._price
...
sortedElements = sorted(elements, key = Dummy.getPrice)
See http://wiki.python.org/moin/HowTo/Sorting/ for more.
Alternatively you can use operator.attrgetter()
:
list_of_objects.sort(key=operator.attrgetter('name_of_attribute_to_sort_by'))
To sort in-place you can use the .sort
method of list
using a function that defines the key to sort on.
>>> class Data(object):
... def __init__(self,x,y):
... self.x=x
... self.y=y
...
>>> l=[Data(i,i+1) for i in xrange(10,-1,-1)]
>>> print ", ".join("%s %s"%(x.x,x.y) for x in l)
10 11, 9 10, 8 9, 7 8, 6 7, 5 6, 4 5, 3 4, 2 3, 1 2, 0 1
>>> l.sort(key=lambda obj:obj.y)
>>> print ", ".join("%s %s"%(x.x,x.y) for x in l)
0 1, 1 2, 2 3, 3 4, 4 5, 5 6, 6 7, 7 8, 8 9, 9 10, 10 11
To get another list
while keeping the original one intact, use the sorted
function with the optional key
parameter defined similarly.
The best place to look is http://wiki.python.org/moin/HowTo/Sorting
Personally, the class __cmp__ function is much handier when dealing with classes, since typically you always want to sort them the same way.
Here's some quick examples:
class Foo :
def __init__(self, x, y) :
self.x = x
self.y = y
def __cmp__(self, x) :
return cmp(self.x, x)
def __repr__(self) :
return "Foo(%d)" % self.x
# Simple list of objects
data = [
Foo(1, 99),
Foo(5, 94),
Foo(6, 93),
Foo(2, 97),
Foo(4, 95),
Foo(3, 96),
]
# sort using the __cmp__ class method - in numeric order
print sorted(data)
# sort using the key lambda, which reverse sorts...
print sorted(data, key=lambda a : a.y)
精彩评论