Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #86855
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: Sort list of dictionaries |
| Date | 2015-03-03 18:44 +0100 |
| Organization | None |
| References | (1 earlier) <mailman.56.1425321135.13471.python-list@python.org> <946797be-10e6-433b-9411-2db0d5697ac8@googlegroups.com> <8ccd65d8-3f1f-41ac-9092-9b0832d2fc49@googlegroups.com> <mailman.61.1425324201.13471.python-list@python.org> <432a0d61-a8ba-46ca-bc53-318e234fe168@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.23.1425404702.21433.python-list@python.org> (permalink) |
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['version'].split(".")])) ),
>> > reverse=True)
>>
>> This lightbulb will break with version numbers > 99999 ;)
>>
>> Here are two alternatives:
>>
>> result = sorted(
>> mylist,
>> key=lambda elem: (elem['name'], LooseVersion(elem['version'])),
>> reverse=True)
>>
>> result = sorted(
>> mylist,
>> key=lambda e: (e["name"], tuple(map(int, e["version"].split(".")))),
>> reverse=True)
>>
>>
>> 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 don't you prefer to use a
> lambda?
I find
def name_version(elem):
return elem['name'], LooseVersion(elem['version'])
more readable than
lambda elem: (elem['name'], LooseVersion(elem['version']))
and I can understand what
>> result = sorted(mylist, key=name_version, reverse=True)
is supposed to do without grokking the implementation of name_version()
first. I can spot a potential bug -- are the names really supposed to occur
in reverse order, not just the versions? -- again without a look at the
implementation.
If I intend to use the script more than once or if I want to rely on the
result I can write unit tests for name_version() to increase confidence that
it does what I expect.
Finally I can add a docstring to make it more discoverable in the
interactive interpreter.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Sort list of dictionaries Charles Heizer <ceh329@gmail.com> - 2015-03-02 10:17 -0800
Re: Sort list of dictionaries Emile van Sebille <emile@fenx.com> - 2015-03-02 10:31 -0800
Re: Sort list of dictionaries Charles Heizer <ceh329@gmail.com> - 2015-03-02 10:38 -0800
Re: Sort list of dictionaries Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-02 11:55 -0700
Re: Sort list of dictionaries Charles Heizer <ceh329@gmail.com> - 2015-03-02 10:58 -0800
Re: Sort list of dictionaries Peter Otten <__peter__@web.de> - 2015-03-02 20:23 +0100
Re: Sort list of dictionaries Charles Heizer <ceh329@gmail.com> - 2015-03-03 07:56 -0800
Re: Sort list of dictionaries Chris Angelico <rosuav@gmail.com> - 2015-03-04 03:09 +1100
Re: Sort list of dictionaries Paul Moore <p.f.moore@gmail.com> - 2015-03-03 08:48 -0800
Re: Sort list of dictionaries Peter Otten <__peter__@web.de> - 2015-03-03 18:44 +0100
Re: Sort list of dictionaries Dave Angel <davea@davea.name> - 2015-03-02 13:59 -0500
Re: Sort list of dictionaries Jason Friedman <jsf80238@gmail.com> - 2015-03-02 22:33 -0700
Re: Sort list of dictionaries Chris Angelico <rosuav@gmail.com> - 2015-03-03 18:07 +1100
Re: Sort list of dictionaries Jason Friedman <jsf80238@gmail.com> - 2015-03-03 07:45 -0700
Re: Sort list of dictionaries Chris Angelico <rosuav@gmail.com> - 2015-03-04 01:50 +1100
Re: Sort list of dictionaries Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-03 18:55 +1100
csiph-web