Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #106424

Re: Sorting a list

From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: Sorting a list
Date 2016-04-04 08:56 +0200
Organization None
Message-ID <mailman.2.1459753001.32530.python-list@python.org> (permalink)
References <ndrn97$bco$1@dont-email.me> <mailman.408.1459711883.28225.python-list@python.org> <ndrt29$36l$1@dont-email.me> <ndt36r$oi$1@ger.gmane.org>

Show all headers | View raw


DFS wrote:

> On 4/3/2016 3:31 PM, Peter Otten wrote:

>>>>> from operator import itemgetter as get
>>>>> print "\n".join("{1} {0}".format(*p) for p in sorted(
>> ... sorted(colCounts, key=get(1)), key=get(0), reverse=True))
> 
> Kind of clunky looking.  Is that why don't you recommend it?

That, and you produce two intermediate lists and an intermediate string. For 
larger input data this may produce significant overhead.

>> You could also cheat and use
>>
>> lambda v: (-v[0], v[1])
>>
>> and a single sorted().
> 
> That works well.  Why is it 'cheating'?

On second thought it isn't ;)

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Sorting a list DFS <nospam@dfs.com> - 2016-04-03 14:30 -0400
  Re: Sorting a list DFS <nospam@dfs.com> - 2016-04-03 14:34 -0400
  Re: Sorting a list Peter Otten <__peter__@web.de> - 2016-04-03 21:31 +0200
    Re: Sorting a list DFS <nospam@dfs.com> - 2016-04-03 16:08 -0400
      Re: Sorting a list Peter Otten <__peter__@web.de> - 2016-04-04 08:56 +0200
      Re: Sorting a list Random832 <random832@fastmail.com> - 2016-04-04 09:40 -0400
      Re: Sorting a list Peter Otten <__peter__@web.de> - 2016-04-04 16:12 +0200

csiph-web