Should I use a dictionary?

John Roth johnroth at ameritech.net
Tue Jun 26 11:08:57 EDT 2001


"Remco Gerlich" <scarblac at pino.selwerd.nl> wrote in message
news:slrn9jgd4o.n2o.scarblac at pino.selwerd.nl...
> Gustaf Liljegren <gustafl at algonet.se> wrote in comp.lang.python:
> > I have an object type for e-mail messages based on the rfc822 module.
After
> > picking out some messages from several sources, I want to sort them in
> > order of the date property of the message object (as far as I
understand,
> > everything in this process needs to be kept in memory). Anyway, the
problem
> > is that I need some hints about how to sort the messages, as they are
not
> > sorted in way I get them.
> >
> > One idea I came up with was to put the date property of the message
object
> > as a key in a dictionary, and let the rest of the message be the value!
But
> > even if it would work, I think it's ugly. If someone can convince me
that
> > it's not ugly, or give a better (i.e. faster) solution, I'd be happy.
>
> Although dictionaries are neat, I don't see how they would work here - you
> can't sort a dictionary, and two messages may have the same date.

You extract the keys into a list, sort them, and then use the sorted keys to
reference the dictionary. As far as duplicates go, I find that using the
sequence
in which I added stuff is adequate - and it eliminates any questions about
whether
the sort is stable. It's very easy in Python - rather than keeping a
counter, you
can use len(dictionary) to get a usable number.

John Roth

>
> Often, if you want to sort things on some field, you want the "decorate,
> sort, undecorate" pattern (aka Schwartzian transform). In this case, if
you
> have a list of messages, you want to change it into a list of (date,
> message) tuples, sort that, then change it back into a list of messages.

As a previous poster said, using that could be quite expensive - it has to
look
at the actual message bodies to determine the order.

> Something like
>
> messages = [...]
> decorated = [ (get_date_in_seconds(message), message) for message in
messages]
> decorated.sort()
> messages = [ part[1] for part in decorated ]
>
> --
> Remco Gerlich





More information about the Python-list mailing list