PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of Kent Johnson
Sent: Wednesday, April 13, 2005 3:52 AM
Cc: tutor@python.org
Subject: Re: [Tutor] sorting a list of dictionaries
-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Sent: Thursday, December 09, 2004 12:19 PM
Satellite L.L.C.
9601 S. Meridian Blvd.
Englewood, CO 80112
Desk: 720-514-5708
-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of Kent Johnson
Sent: Wednesday, April 13, 2005 3:52 AM
Cc: tutor@python.org
Subject: Re: [Tutor] sorting a list of di
-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Sent: Thursday, December 09, 2004 12:19 PM
To: tutor@python.org
Subject: Re: [Tutor] sorting a list of dictionaries
On 9 Dez 2004, [EMAIL PROTECTED] wrote:
I have a list of dictionaries, each representing info about a
Gooch, John wrote:
I am working on a dictionary sorting problem just like the one in the email
thread at the bottom of this message. My question about their solution is:
In these lines:
lst.sort(lambda m, n: cmp(m.get(field), n.get(field)))
where field is either 'name' or 'size'.
Wh
;.
What is "n:" and what is "lambda m" ?
Thank You,
John A. Gooch
-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Sent: Thursday, December 09, 2004 12:19 PM
To: tutor@python.org
Subject: Re: [Tutor] sorting a list of dictionaries
On 9 Dez 20
On Thu, Dec 09, 2004 at 03:22:29PM -0500, Kent Johnson wrote:
> Using sort() with a user compare function is not recommended when you
> care about performance. The problem is that the sort function has to
> call back into Python code for every compare, of which there are many.
> The decorate - sort
Using sort() with a user compare function is not recommended when you care about performance. The
problem is that the sort function has to call back into Python code for every compare, of which
there are many. The decorate - sort - undecorate idiom is the preferred way to do this in Python <
2.4
On 9 Dez 2004, [EMAIL PROTECTED] wrote:
> I have a list of dictionaries, each representing info about a file,
> something like:
>
> [{'name':'foo.txt','size':35}, {'name':'bar.txt','size':35}, ...]
>
> I want to present a sorted list of all the files' data, sorting on the
> keys 'name' or 'size'.
If you can use Python 2.4 it is very simple using the new key= parameter to sort and
operator.itemgetter:
>>> import operator
>>> ds = [{'name':'foo.txt','size':35}, {'name':'bar.txt','size':36}]
>>> ds.sort(key=operator.itemgetter('name'))
>>> ds
[{'name': 'bar.txt', 'size': 36}, {'name': 'foo.t