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


Groups > comp.lang.python > #80504

Re: "Natural" use of cmp= in sort

Path csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'argument': 0.05; 'debugging': 0.07; 'detect': 0.07; 'modified': 0.07; 'ugly': 0.07; 'answering': 0.09; 'function,': 0.09; 'merging': 0.09; 'seemed': 0.09; 'cmp': 0.16; 'cmp=': 0.16; 'cyclic': 0.16; 'ian.': 0.16; 'implies': 0.16; 'key=': 0.16; 'newsgroups': 0.16; 'utc,': 0.16; 'wrote:': 0.18; "skip:' 30": 0.19; 'seems': 0.21; '>>>': 0.22; 'input': 0.22; 'mon,': 0.24; 'looks': 0.24; 'question': 0.24; 'sort': 0.25; 'pass': 0.26; 'post': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'compared': 0.30; 'raymond': 0.30; 'relative': 0.30; 'see,': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'posting': 0.31; 'comparison': 0.31; 'produces': 0.31; 'routine': 0.31; 'there.': 0.32; 'run': 0.32; 'another': 0.32; '(i.e.': 0.33; 'cases': 0.33; 'monday,': 0.33; 'trouble': 0.34; 'agree': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'consistent': 0.36; 'thanks': 0.36; 'hi,': 0.36; 'nov': 0.38; 'question,': 0.38; 'to:addr:python-list': 0.38; 'issue': 0.38; 'pm,': 0.38; 'rather': 0.38; 'itself': 0.39; 'subject:" ': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'relations': 0.60; 'results.': 0.60; 'first': 0.61; 'information': 0.63; 'such': 0.63; 'decided': 0.64; 'provide': 0.64; 'more': 0.64; 'total': 0.65; 'natural': 0.68; 'subject': 0.69; 'results': 0.69; 'url:a': 0.72; 'partial': 0.84; 'peter,': 0.84
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; bh=ChGQwaFK75RutRXX+IHf0NLIXsLf3TKM3pWAWiOrDbQ=; b=I2KLx+7uEwwo9ZEgVPRwVtT3wpM/SwVPUwWb5nw2/Ak33Msy0w9ZCXOhevv4Y1vnXI Pf5k5pfc1GUebZSPiFA3kfQxO402eZHVcnL4oGKvZdWNXJ9ITmbcfqXmiQafM1ly2bxm LDQWp5qGgi1PNLZ0qxmHReiSybq2dFIFUjv9P2Z7Y2eINzsNYMgppqUhbdcVflImw0kI qPmjH+V5zENWabf9fZjGinLD2ssr8uVF6t/1JkXqaPL4/Myj3S0D0/FSZXs76wL2gnsV w/6yCVwTBfisd0SJuLsbbBShPE+3RUrMEgqQhzuzGB574qmLleqMgdsPrfWlfe8j6Cyn fWXw==
X-Received by 10.70.91.10 with SMTP id ca10mr38715361pdb.6.1415687810472; Mon, 10 Nov 2014 22:36:50 -0800 (PST)
MIME-Version 1.0
In-Reply-To <fc99182d-8779-460d-90b7-4e9a8ff9c548@googlegroups.com>
References <eb697764-934b-4650-af28-1c55520bc1da@googlegroups.com> <fc99182d-8779-460d-90b7-4e9a8ff9c548@googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Mon, 10 Nov 2014 23:36:10 -0700
Subject Re: "Natural" use of cmp= in sort
To Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.15647.1415687819.18130.python-list@python.org> (permalink)
Lines 49
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1415687819 news.xs4all.nl 2920 [2001:888:2000:d::a6]:35346
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:80504

Show key headers only | View raw


On Mon, Nov 10, 2014 at 8:09 PM, Paddy <paddy3118@gmail.com> wrote:
> On Monday, 10 November 2014 18:45:15 UTC, Paddy  wrote:
>> Hi, I do agree with                                                                          Raymond H. about the relative merits of cmp= and key= in sort/sorted, but I decided to also not let natural uses of cmp= pass silently.
>>
>> In answering this question, http://stackoverflow.com/a/26850434/10562 about ordering subject to inequalities it seemed natural to use the cmp= argument of sort rather than key=.
>>
>> The question is about merging given inequalities to make 1 inequality such that the inequalities also stays true.
>>
>>
>
> Thanks Peter, Ian. I have modified my code to expand transitive relations and ask you to view it on stackoverflow via the original link (as posting code on newsgroups is an ugly hack).

You still run into trouble though if the given inequalities don't
provide enough information for a total ordering. E.g.:

>>> ' > '.join(extract_relations("""f4 > f1
... f2 > f3"""))
'f1 > f2 > f3 > f4'

By adding some debugging prints, we can see what cmp calls were made
by the sort routine and what the results were:

cmp('f2', 'f1') -> 1
cmp('f3', 'f2') -> 1
cmp('f4', 'f3') -> 1

There is no information about the relative order of f2 and f1, so the
cmp function just returns 1 there.
f2 is known to be greater than f3, so that call correctly returns 1.
There is again no information about the relative order of f4 and f3,
so it again just returns 1. However, this is inconsistent with the
first comparison that placed f1 > f2, because it implies that f1 > f4.

As you can see, giving an inconsistent cmp function to sort produces
bogus results. If you only have a partial ordering of the inputs, you
need to make sure that the cmp function you provide is consistent with
*some* total ordering.

Another issue is that your expand_transitive_relations function is I
think O(n**3 log n), which looks unattractive compared to the O(n**2)
topological sort given in the other answers. Another advantage of the
topological sort is that it will detect if the graph is cyclic (i.e.
the input data itself is inconsistent), rather than just return a
bogus output.

> My main reason for the post to c.l.p remains though; it seems like a *natural* use of the cmp= comparator function to sorted rather than using key= .

There are cases where a cmp function is more natural than a key
function, but for these we have the functools.cmp_to_key adapter.

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


Thread

"Natural" use of cmp= in sort Paddy <paddy3118@gmail.com> - 2014-11-10 10:45 -0800
  Re: "Natural" use of cmp= in sort Peter Otten <__peter__@web.de> - 2014-11-10 20:19 +0100
  Re: "Natural" use of cmp= in sort Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-10 12:43 -0700
    Re: "Natural" use of cmp= in sort Paddy <paddy3118@gmail.com> - 2014-11-10 18:03 -0800
  Re: "Natural" use of cmp= in sort Paddy <paddy3118@gmail.com> - 2014-11-10 19:09 -0800
    Re: "Natural" use of cmp= in sort Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-10 23:36 -0700
      Re: "Natural" use of cmp= in sort Paddy <paddy3118@gmail.com> - 2014-11-10 23:44 -0800
        Re: "Natural" use of cmp= in sort Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-11 01:35 -0700
          Re: "Natural" use of cmp= in sort Paddy <paddy3118@gmail.com> - 2014-11-11 01:21 -0800
            Re: "Natural" use of cmp= in sort Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-11 10:59 -0700
              Re: "Natural" use of cmp= in sort Paddy <paddy3118@gmail.com> - 2014-11-12 00:09 -0800

csiph-web