Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #86855
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.008 |
| X-Spam-Evidence | '*H*': 0.98; '*S*': 0.00; 'interpreter.': 0.07; '%s"': 0.09; 'mind,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; 'bug': 0.12; 'charles': 0.16; 'docstring': 0.16; 'elem:': 0.16; 'lambda': 0.16; 'lambda:': 0.16; 'personally,': 0.16; 'readable': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:Sort': 0.16; 'subject:dictionaries': 0.16; 'wrote:': 0.18; 'first.': 0.19; 'tests': 0.22; 'header:User- Agent:1': 0.23; 'skip:l 30': 0.24; 'script': 0.25; 'header:X -Complaints-To:1': 0.27; 'subject:list': 0.30; 'skip:( 20': 0.30; 'went': 0.31; 'supposed': 0.32; 'monday,': 0.33; 'add': 0.35; 'version': 0.36; 'really': 0.36; 'unit': 0.37; 'two': 0.37; 'being': 0.38; 'thank': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'break': 0.61; 'new': 0.61; 'march': 0.61; 'numbers': 0.61; 'you.': 0.62; 'skip:n 10': 0.64; 'more': 0.64; 'finally': 0.65; 'occur': 0.65; 'spot': 0.65; 'here': 0.66; 'reverse': 0.68; 'increase': 0.74; '2015': 0.84; 'otten': 0.84; 'peter,': 0.84; 'confidence': 0.95 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: Sort list of dictionaries |
| Date | Tue, 03 Mar 2015 18:44:51 +0100 |
| Organization | None |
| References | <f1a339d0-0386-43ba-b6b4-1aee39d75581@googlegroups.com> <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> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p57bd92a5.dip0.t-ipconnect.de |
| User-Agent | KNode/4.13.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.19 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.23.1425404702.21433.python-list@python.org> (permalink) |
| Lines | 61 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1425404702 news.xs4all.nl 2856 [2001:888:2000:d::a6]:48528 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:86855 |
Show key headers only | View raw
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