By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,221 Members | 1,604 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,221 IT Pros & Developers. It's quick & easy.

Using s.sort([cmp[, key[, reverse]]]) to sort a list of objects based on a attribute

P: n/a
Hi there

I am fairly new to Python and have not really used regular expressions
before (I think this might be needed for my query) and wondered if you
could help

I have a step class and store in a list step instances
A step instance contains variables: name, startTime etc and startTime
is stored as a string %H:%M:%S

What I would like to do is to be able to sort this list of objects
based on the startTime object so that the first item in the list is
the object with the earliest Start time and last item is the object
with the last Start time.

I belive my key has to be = strpTime(step.sTime, "%H:%M:%S")
But don't know how to create the comparison funciton.

Any help on how I can perform this whole operation would be much
appreciated.

Thanks
Chris

Sep 7 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Fri, 07 Sep 2007 06:57:35 -0700, cjt22 wrote:
I have a step class and store in a list step instances
A step instance contains variables: name, startTime etc and startTime
is stored as a string %H:%M:%S

What I would like to do is to be able to sort this list of objects
based on the startTime object so that the first item in the list is
the object with the earliest Start time and last item is the object
with the last Start time.

I belive my key has to be = strpTime(step.sTime, "%H:%M:%S")
But don't know how to create the comparison funciton.

Any help on how I can perform this whole operation would be much
appreciated.
This should be enough::

steps.sort(key=lambda s: s.startTime)

If you sort strings of the form 'hh:mm:ss' the represented times are
sorted chronological. No need to convert them to a number first.

If the "natural" sort criterion for `Step` objects is the start time you
might override `__cmp__()` of `Step`\s instead::

def __cmp__(self, other):
return cmp(self.startTime, other.startTime)

Now you can just sort the list with ``steps.sort()``.

Ciao,
Marc 'BlackJack' Rintsch
Sep 7 '07 #2

P: n/a
steps.sort(key = lambda s: s.time)
This is why attrgetter in the operator module was invented.
from operator import attrgetter
....
steps.sort(key=attrgettr("time"))

HTH,
--
Miki <mi*********@gmail.com>
http://pythonwise.blogspot.com

Sep 9 '07 #3

P: n/a
Miki <mi*********@gmail.comwrites:
steps.sort(key = lambda s: s.time)
This is why attrgetter in the operator module was invented.
from operator import attrgetter
...
steps.sort(key=attrgettr("time"))
Personally I prefer the anonymous function over attrgettr :)

S.
Sep 9 '07 #4

P: n/a
Stefan Arentz <st***********@gmail.comwrote:
Miki <mi*********@gmail.comwrites:
steps.sort(key = lambda s: s.time)
This is why attrgetter in the operator module was invented.
from operator import attrgetter
...
steps.sort(key=attrgettr("time"))

Personally I prefer the anonymous function over attrgettr :)
However, Python disagrees with you...:

brain:~ alex$ python -mtimeit -s'from operator import attrgetter;
L=map(complex,xrange(999))' 'sorted(L, key=lambda x:x.real)'
1000 loops, best of 3: 567 usec per loop

brain:~ alex$ python -mtimeit -s'from operator import attrgetter;
L=map(complex,xrange(999))' 'sorted(L, key=attrgetter("real"))'
1000 loops, best of 3: 367 usec per loop

A speed-up of 35% is a pretty clear indicator of what _Python_ "prefers"
in this situation:-).
Alex
Sep 9 '07 #5

P: n/a
al***@mac.com (Alex Martelli) writes:
Stefan Arentz <st***********@gmail.comwrote:
Miki <mi*********@gmail.comwrites:
steps.sort(key = lambda s: s.time)
This is why attrgetter in the operator module was invented.
from operator import attrgetter
...
steps.sort(key=attrgettr("time"))
Personally I prefer the anonymous function over attrgettr :)

However, Python disagrees with you...:

brain:~ alex$ python -mtimeit -s'from operator import attrgetter;
L=map(complex,xrange(999))' 'sorted(L, key=lambda x:x.real)'
1000 loops, best of 3: 567 usec per loop

brain:~ alex$ python -mtimeit -s'from operator import attrgetter;
L=map(complex,xrange(999))' 'sorted(L, key=attrgetter("real"))'
1000 loops, best of 3: 367 usec per loop

A speed-up of 35% is a pretty clear indicator of what _Python_ "prefers"
in this situation:-).
I could not care less :-)

S.
Sep 10 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.