Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'argument': 0.05; 'suppose': 0.07; 'assumed': 0.09; 'function:': 0.09; 'output,': 0.09; 'sentence': 0.09; 'python': 0.11; '(it': 0.16; 'argument.': 0.16; 'ascending': 0.16; 'cmp': 0.16; 'descending': 0.16; 'expression,': 0.16; 'formed,': 0.16; 'i.e.,': 0.16; 'ian.': 0.16; 'inputs': 0.16; 'key=': 0.16; 'mean,': 0.16; 'objects.': 0.16; 'secondarily': 0.16; 'squirrel': 0.16; 'wrote:': 0.18; 'basically': 0.19; 'input': 0.22; 'example': 0.22; 'sort': 0.25; 'second': 0.26; 'pass': 0.26; 'values': 0.27; 'header:In-Reply- To:1': 0.27; 'idea': 0.28; 'function': 0.29; 'correct': 0.29; 'am,': 0.29; 'originally': 0.30; 'primarily': 0.30; 'message- id:@mail.gmail.com': 0.30; 'another.': 0.31; 'bunch': 0.31; 'comparison': 0.31; 'safely': 0.31; 'class': 0.32; 'this.': 0.32; 'cases': 0.33; 'table': 0.34; 'could': 0.34; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'really': 0.36; 'ordered': 0.36; 'thanks': 0.36; 'two': 0.37; 'easily': 0.37; 'expressed': 0.37; 'implement': 0.38; 'mapping': 0.38; 'nov': 0.38; 'to:addr:python-list': 0.38; 'track': 0.38; 'previous': 0.38; 'subject:" ': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'algorithms': 0.60; 'relations': 0.60; 'simple': 0.61; 'first': 0.61; 'map': 0.64; 'more': 0.64; 'natural': 0.68; 'reverse': 0.68; 'ordered.': 0.84; 'partially': 0.84; 'route': 0.84; 'approach.': 0.91; 'few.': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=0B9wyg3mN66NTMlfmc2Cf4YC2uVZ0gHTjctVxjogkU0=; b=b6IXbDJVbtHacuuuMUTtlCOU2FDBVNDSk8CgZl0HusNtBsvvnqmfMR3q6oqqS0oCIu 0Ob/g3WTHIohdSzaPtKNy05P080qt4mjQ2hD5mAeeRS8GTCuj8dAMHflyJGVim3bmxhN 1LwmWAp8dvP5Nu5slDe+EonQ9L11oeGOA0FGTCUSsR5L6/elbU4tjIQGGbkiU2FZ2AqX T57iL2wRt4tgUdtkgnlO80OHJEsxJlMvqMKXnEyMECjOAzRub160+GGyULsFXJNhTcy9 NiAJvguO1Hw0Crp9os7umXkXEJvxpXaYdW7Kfi9Q/A1EcujmjaONG1PpEmPnTAUAQtyK EWdQ== X-Received: by 10.68.93.1 with SMTP id cq1mr9991099pbb.145.1415694974950; Tue, 11 Nov 2014 00:36:14 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Tue, 11 Nov 2014 01:35:34 -0700 Subject: Re: "Natural" use of cmp= in sort To: Python Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1415696821 news.xs4all.nl 2977 [2001:888:2000:d::a6]:37060 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80514 On Tue, Nov 11, 2014 at 12:44 AM, Paddy wrote: > Thanks Ian. The original author states "...and it is sure that the given = inputs will give an output, i.e., the inputs will always be valid.", which = could be taken as meaning that all inputs are sufficient, well formed, and = contain all relations as their first example does. Well, I brought it up because the start of that sentence is "There can be multiple inequalities as answer but I need any one which is correct...". The only way there would be more than one correct answer would be if the inputs were only partially ordered. I take the second part of the sentence as meaning only that the input can be safely assumed to be consistent. > Yes, I knew that there are cases where a cmp function is more natural tha= n key; the idea is to squirrel out a few. We have already made the, (well r= easoned in my opinion), decision to go down the key=3D route in Python 3. I= also like to track where my algorithms might originally map to cmp=3D. (It= is not often). Basically any time you have a comparison that isn't easily expressed by mapping the values to some bunch of ordered objects. As an example of what I mean, we can easily sort alphabetically using a key function: sorted(items, key=3Dstr) And we can easily sort reverse alphabetically: sorted(items, key=3Dstr, reverse=3DTrue) And we can easily sort alphabetically on multiple criteria: sorted(items, key=3Dlambda x: (str(x.last_name), str(x.first_name))) But what if we want to combine those last two requirements? Suppose we have a table that we want to present sorted primarily in ascending order on one column, and secondarily in descending order on another. We can't just add the reverse argument argument to the previous expression, because that would reverse the sort order for both columns. But there is no simple key function for strings that will reverse the sort without using the reverse argument. There are basically two ways to approach this. One is to implement a cmp function and pass it to cmp_to_key. The other is to create a class with comparison methods that result in the desired order, and use the class as the key; this is really just a variation ot the cmp_to_key approach.