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'.
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 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'. The file 'name' s should be unique (I'm hoping) > across all the dictionaries. Can someone point me towards an efficient > solution for accomplishing the sort? (The list has 1000s of files). That's easy to achieve, since sort takes a custom sort function as optional argument. Now you need only a function which takes the values of the fileds and compares them. E.g. lst.sort(lambda m, n: cmp(m.get(field), n.get(field))) where field is either 'name' or 'size'. As a function: def sort_it (lst, field): lst.sort(lambda m, n: cmp(m.get(field), n.get(field))) Karl -- Please do *not* send copies of replies to me. I read the list _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor