Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Sort list of dictionaries by key (case insensitive)

Reply
Thread Tools

Re: Sort list of dictionaries by key (case insensitive)

 
 
Nico Grubert
Guest
Posts: n/a
 
      01-13-2010

> Er, that should have been mylist.sort(key = lambda d:
> d['title'].lower()) of course.



Thanks a lot for the tip, chris.
Unfortunately, I only have Python 2.3.5 installed and can't upgrade to
2.4 due to an underliying application server.

In python 2.3 the 'sort()' function does not excepts any keywords
arguments (TypeError: sort() takes no keyword arguments), so is there a
workaround?

Regards
Nico
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      01-13-2010
Nico Grubert wrote:

>> Er, that should have been mylist.sort(key = lambda d:
>> d['title'].lower()) of course.

>
>
> Thanks a lot for the tip, chris.
> Unfortunately, I only have Python 2.3.5 installed and can't upgrade to
> 2.4 due to an underliying application server.
>
> In python 2.3 the 'sort()' function does not excepts any keywords
> arguments (TypeError: sort() takes no keyword arguments), so is there a
> workaround?


There is a technique called decorate-sort-undecorate:

>>> def sorted(items, key):

.... decorated = [(key(item), index, item) for index, item in
enumerate(items)]
.... decorated.sort()
.... return [item[2] for item in decorated]
....
>>> items = "Atem Äther ähnlich anders".split()
>>> print " ".join(sorted(items, key=lambda s: s.lower()))

anders Atem Äther ähnlich
>>> print " ".join(sorted(items, key=lambda s: locale.strxfrm(s)))

ähnlich anders Atem Äther

The above may run on 2.3, but I actually ran it on 2.6.

Peter

 
Reply With Quote
 
 
 
 
Stefan Behnel
Guest
Posts: n/a
 
      01-13-2010
Peter Otten, 13.01.2010 13:25:
> >>> items = "Atem ther hnlich anders".split()
> >>> print " ".join(sorted(items, key=lambda s: s.lower()))


If you can make sure that 's' is either always a byte string or always a
unicode string (which is good programming practice anyway), an unbound
method can simplify (and speed up) the above, e.g.

sorted(items, key=unicode.lower)

Stefan
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Sort list of dictionaries by key (case insensitive) Nico Grubert Python 2 01-13-2010 03:37 PM
Sort list of dictionaries by key (case insensitive) Nico Grubert Python 2 01-13-2010 11:11 AM
updating dictionaries from/to dictionaries Brandon Python 12 08-15-2008 12:35 AM
Pickling dictionaries containing dictionaries: failing,recursion-style! lysdexia Python 6 12-02-2007 12:03 AM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM



Advertisments