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
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},
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
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
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}
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
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()'
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
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]
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
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)]
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
12 matches
Mail list logo