Re: Sort list of dictionaries

2015-03-03 Thread Peter Otten
Charles Heizer wrote: > On Monday, March 2, 2015 at 11:23:37 AM UTC-8, Peter Otten wrote: >> Charles Heizer wrote: >> >> > Never mind, the light bulb finally went off. :-\ >> > >> > sortedlist = sorted(mylist , key=lambda elem: "%s %s" % ( elem['name'], >> > (".".join([i.zfill(5) for i in elem['

Re: Sort list of dictionaries

2015-03-03 Thread Paul Moore
On Tuesday, 3 March 2015 16:09:31 UTC, Chris Angelico wrote: > On Wed, Mar 4, 2015 at 2:56 AM, Charles Heizer wrote: > >> Personally, I prefer to not use a lambda: > >> > >> def name_version(elem): > >> return elem['name'], LooseVersion(elem['version']) > >> > >> result = sorted(mylist, key=na

Re: Sort list of dictionaries

2015-03-03 Thread Chris Angelico
On Wed, Mar 4, 2015 at 2:56 AM, Charles Heizer wrote: >> Personally, I prefer to not use a lambda: >> >> def name_version(elem): >> return elem['name'], LooseVersion(elem['version']) >> >> result = sorted(mylist, key=name_version, reverse=True) > > Peter, thank you. Me being new to Python why

Re: Sort list of dictionaries

2015-03-03 Thread Charles Heizer
On Monday, March 2, 2015 at 11:23:37 AM UTC-8, Peter Otten wrote: > Charles Heizer wrote: > > > Never mind, the light bulb finally went off. :-\ > > > > sortedlist = sorted(mylist , key=lambda elem: "%s %s" % ( elem['name'], > > (".".join([i.zfill(5) for i in elem['version'].split(".")])) ), > >

Re: Sort list of dictionaries

2015-03-03 Thread Chris Angelico
On Wed, Mar 4, 2015 at 1:45 AM, Jason Friedman wrote: > I appreciate > how Python makes my job easier by doing much of my work for me. A > colleague yesterday asked how I guaranteed my temporary file names > would not collide with another file, and my answer was I don't have to > worry about it,

Re: Sort list of dictionaries

2015-03-03 Thread Jason Friedman
On Tue, Mar 3, 2015 at 12:07 AM, Chris Angelico wrote: > Heh, I think that mght be a bit abusive :) I'm not sure that > you want to depend on the version numbers fitting inside the rules for > IP addresses, especially given that the example has a component of > "2214". > Indeed, I was mi

Re: Sort list of dictionaries

2015-03-03 Thread Steven D'Aprano
Charles Heizer wrote: > Hello, > I'm new to python and I'm trying to find the right way to solve this issue > I have. > > I'm trying to sort this list by name and then by version numbers. The > problem I'm having is that I can not get the version numbers sorted with > the highest at the top or so

Re: Sort list of dictionaries

2015-03-02 Thread Chris Angelico
On Tue, Mar 3, 2015 at 4:33 PM, Jason Friedman wrote: >>> This is what I was trying but LooseVersion() was not sorting version >>> numbers like I thought it would. You will notice that Chrome version >>> "40.0.2214.111" is higher than "40.0.2214.91" but in the end result it's >>> not sorting it

Re: Sort list of dictionaries

2015-03-02 Thread Jason Friedman
>> This is what I was trying but LooseVersion() was not sorting version numbers >> like I thought it would. You will notice that Chrome version "40.0.2214.111" >> is higher than "40.0.2214.91" but in the end result it's not sorting it that >> way. > > Because it's a string they're sorted lexicog

Re: Sort list of dictionaries

2015-03-02 Thread Peter Otten
Charles Heizer wrote: > Never mind, the light bulb finally went off. :-\ > > sortedlist = sorted(mylist , key=lambda elem: "%s %s" % ( elem['name'], > (".".join([i.zfill(5) for i in elem['version'].split(".")])) ), > reverse=True) This lightbulb will break with version numbers > 9 ;) Here a

Re: Sort list of dictionaries

2015-03-02 Thread Charles Heizer
Never mind, the light bulb finally went off. :-\ sortedlist = sorted(mylist , key=lambda elem: "%s %s" % ( elem['name'], (".".join([i.zfill(5) for i in elem['version'].split(".")])) ), reverse=True) On Monday, March 2, 2015 at 10:40:30 AM UTC-8, Charles Heizer wrote: > Sorry, > > sortedlist = s

Re: Sort list of dictionaries

2015-03-02 Thread Dave Angel
On 03/02/2015 01:38 PM, Charles Heizer wrote: Sorry, sortedlist = sorted(mylist , key=lambda elem: "%s %s" % (elem['name'], LooseVersion(elem['version'])), reverse=True) This is what I was trying but LooseVersion() was not sorting version numbers like I thought it would. You will notice that

Re: Sort list of dictionaries

2015-03-02 Thread Ian Kelly
On Mon, Mar 2, 2015 at 11:38 AM, Charles Heizer wrote: > Sorry, > > sortedlist = sorted(mylist , key=lambda elem: "%s %s" % (elem['name'], > LooseVersion(elem['version'])), reverse=True) > > This is what I was trying but LooseVersion() was not sorting version numbers > like I thought it would. Y

Re: Sort list of dictionaries

2015-03-02 Thread Charles Heizer
Sorry, sortedlist = sorted(mylist , key=lambda elem: "%s %s" % (elem['name'], LooseVersion(elem['version'])), reverse=True) This is what I was trying but LooseVersion() was not sorting version numbers like I thought it would. You will notice that Chrome version "40.0.2214.111" is higher than "

Re: Sort list of dictionaries

2015-03-02 Thread Emile van Sebille
On 3/2/2015 10:17 AM, Charles Heizer wrote: Hello, I'm new to python and I'm trying to find the right way to solve this issue I have. I'm trying to sort this list by name and then by version numbers. The problem I'm having is that I can not get the version numbers sorted with the highest at t

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

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 >enumera

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 ac

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] i

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

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 '

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 a

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

2010-01-13 Thread Florian Diesch
Nico Grubert 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

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 wrote: > On Tue, Jan 12, 2010 at 11:45 PM, Nico Grubert wrote: >> Hi there >> >> I have the following list 'mylist' that contains some dictionaries: >> >> mylist = [{'title':'the Fog', 'id':1}, >>          {'title':'The Storm', 'id':2}, >>          {'

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 so

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 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',