Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: Sorting a list Date: Mon, 04 Apr 2016 08:56:26 +0200 Organization: None Lines: 23 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de g9Q2ZUla0O7o+S5ZFkEcNwcAdg9w2Phv00g1uAENEW/Q== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'string.': 0.04; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'intermediate': 0.15; 'dfs': 0.16; 'itemgetter': 0.16; 'lambda': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; 'input': 0.18; 'second': 0.24; 'import': 0.24; 'header:User-Agent:1': 0.26; 'subject:list': 0.26; 'header:X-Complaints-To:1': 0.26; 'print': 0.30; 'lists': 0.34; 'that,': 0.34; 'could': 0.35; "isn't": 0.35; 'skip:{ 10': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:org': 0.37; 'thought': 0.37; 'why': 0.39; 'data': 0.39; 'well.': 0.40; 'to:addr:python.org': 0.40; 'received:de': 0.40; '>>>>>': 0.66; 'otten': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd9cb3.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: Xref: csiph.com comp.lang.python:106424 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 ;)