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

2010-01-13 Thread Chris Rebert
On Tue, Jan 12, 2010 at 11:45 PM, Nico Grubert nicogrub...@gmail.com wrote:
 Hi there

 I have the following list 'mylist' that contains some dictionaries:

 mylist = [{'title':'the Fog', 'id':1},
          {'title':'The Storm', 'id':2},
          {'title':'the bible', 'id':3},
          {'title':'The thunder', 'id':4}
         ]

mylist.sort(key = lambda d: d['title'])

Use operator.itemgetter() to optimize in the unlikely event it becomes
necessary.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2010-01-13 Thread Peter Otten
Nico Grubert wrote:

 I have the following list 'mylist' that contains some dictionaries:
 
 mylist = [{'title':'the Fog', 'id':1},
{'title':'The Storm', 'id':2},
{'title':'the bible', 'id':3},
{'title':'The thunder', 'id':4}
   ]
 
 How I can sort (case insensitive) the list by the dictioary's 'title' key?
 
 The result should be this list:
 [{'title':'the bible', 'id':3},
   {'title':'the Fog', 'id':1},
   {'title':'The Storm', 'id':2},
   {'title':'The thunder', 'id':4}
 ]
 
 I am using Python 2.4.

Python 2.4.6 (#2, Mar 19 2009, 10:02:47)
[GCC 4.3.3] on linux2
Type help, copyright, credits or license for more information.
 import locale
 locale.setlocale(locale.LC_ALL, )
'de_DE.UTF-8'
 mylist = [{'title':'the Fog', 'id':1},
...{'title':'The Storm', 'id':2},
...{'title':'the bible', 'id':3},
...{'title':'The thunder', 'id':4}
...   ]
 mylist.sort(key=lambda item: locale.strxfrm(item[title]))
 import pprint
 pprint.pprint(mylist)
[{'id': 3, 'title': 'the bible'},
 {'id': 1, 'title': 'the Fog'},
 {'id': 2, 'title': 'The Storm'},
 {'id': 4, 'title': 'The thunder'}]

Peter
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2010-01-13 Thread Chris Rebert
On Wed, Jan 13, 2010 at 2:41 AM, Chris Rebert c...@rebertia.com wrote:
 On Tue, Jan 12, 2010 at 11:45 PM, Nico Grubert nicogrub...@gmail.com wrote:
 Hi there

 I have the following list 'mylist' that contains some dictionaries:

 mylist = [{'title':'the Fog', 'id':1},
          {'title':'The Storm', 'id':2},
          {'title':'the bible', 'id':3},
          {'title':'The thunder', 'id':4}
         ]

 mylist.sort(key = lambda d: d['title'])

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

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2010-01-13 Thread Florian Diesch
Nico Grubert nicogrub...@gmail.com writes:

 Hi there

 I have the following list 'mylist' that contains some dictionaries:

 mylist = [{'title':'the Fog', 'id':1},
   {'title':'The Storm', 'id':2},
   {'title':'the bible', 'id':3},
   {'title':'The thunder', 'id':4}
  ]

 How I can sort (case insensitive) the list by the dictioary's 'title' key?

mylist.sort(key=lambda x: x['title'].lower())



   Florian
-- 
http://www.florian-diesch.de/software/easygconf/
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2010-01-13 Thread Nico Grubert



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
--
http://mail.python.org/mailman/listinfo/python-list


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

2010-01-13 Thread Peter Otten
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

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2010-01-13 Thread Stefan Behnel

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
--
http://mail.python.org/mailman/listinfo/python-list


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

2010-01-13 Thread Nico Grubert

Thanks a lot Stefan  Peter.

I'm almost there (except sorting of umlauts does not work yet).


import locale

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 = [{'title':'the Ähnlich', 'id':1},
 {'title':'The Storm', 'id':2},
 {'title':'the bible','id':3},
 {'title':'The thunder', 'id':4}]

print sorted(items, key=lambda d: locale.strxfrm(d.get('title')))

- [{'id': 2, 'title': 'The Storm'}, {'id': 4, 'title': 'The thunder'}, 
{'id': 3, 'title': 'the bible'}, {'id': 1, 'title': 'the \xc4hnlich'}]



The entry with the umlaut is the last item in but according to german 
umlaut rules it should be the first item in the result.

Do I have to set anything with the locale module?


Regards
Nico

--
http://mail.python.org/mailman/listinfo/python-list


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

2010-01-13 Thread Stefan Behnel

Nico Grubert, 13.01.2010 16:18:

print sorted(items, key=lambda d: locale.strxfrm(d.get('title')))

- [{'id': 2, 'title': 'The Storm'}, {'id': 4, 'title': 'The thunder'}, 
{'id': 3, 'title': 'the bible'}, {'id': 1, 'title': 'the \xc4hnlich'}]


The entry with the umlaut is the last item in but according to german 
umlaut rules it should be the first item in the result.

Do I have to set anything with the locale module?


http://wiki.python.org/moin/HowTo/Sorting#Topicstobecovered

Stefan
--
http://mail.python.org/mailman/listinfo/python-list


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

2010-01-13 Thread Peter Otten
Nico Grubert wrote:

 Thanks a lot Stefan  Peter.
 
 I'm almost there (except sorting of umlauts does not work yet).
 
 
 import locale

locale.setlocale(locale.LC_ALL, )

 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 = [{'title':'the Ähnlich', 'id':1},
   {'title':'The Storm', 'id':2},
   {'title':'the bible','id':3},
   {'title':'The thunder', 'id':4}]
 
 print sorted(items, key=lambda d: locale.strxfrm(d.get('title')))
 
 - [{'id': 2, 'title': 'The Storm'}, {'id': 4, 'title': 'The thunder'},
 {'id': 3, 'title': 'the bible'}, {'id': 1, 'title': 'the \xc4hnlich'}]
 
 
 The entry with the umlaut is the last item in but according to german
 umlaut rules it should be the first item in the result.
 Do I have to set anything with the locale module?

Adding the setlocale() call will suffice provided your script uses the same 
encoding as your environment. If not something like

# -*- coding:utf-8 -*-
import locale

locale.setlocale(locale.LC_ALL, )
encoding = locale.getlocale()[1]

def sorted(items, key):
 decorated = [(key(item), index, item) for index, item in
   enumerate(items)]
 decorated.sort()
 return [item[2] for item in decorated]

# book titles use unicode
items = [{'title':u'the Ähnlich', 'id':1},
  {'title':u'The Storm', 'id':2},
  {'title':u'the bible','id':3},
  {'title':u'The thunder', 'id':4}]

def sortkey(item):
s = item[title].encode(encoding)
return locale.strxfrm(s)

print sorted(items, key=sortkey)

may be a bit more robust. If your source code doesn't use UTF-8 you have to 
modify the coding declaration at the top accordingly.

Peter

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2010-01-13 Thread Nico Grubert



http://wiki.python.org/moin/HowTo/Sorting#Topicstobecovered


Works fine. Thanks a lot for your help, Stefan.

Regards
Nico
--
http://mail.python.org/mailman/listinfo/python-list