Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'win32': 0.03; 'argument': 0.05; '"""': 0.07; 'answering': 0.09; 'merging': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'seemed': 0.09; 'works.': 0.09; 'python': 0.11; 'cmp=': 0.16; 'key=': 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; 'set()': 0.16; 'tried:': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'print': 0.22; 'header:User-Agent:1': 0.23; 'question': 0.24; 'sort': 0.25; 'pass': 0.26; 'code:': 0.26; 'header:X-Complaints- To:1': 0.27; 'raymond': 0.30; 'relative': 0.30; "i'm": 0.30; '>>>>': 0.31; 'agree': 0.35; 'but': 0.35; 'hi,': 0.36; 'question,': 0.38; 'skip:. 20': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'skip:. 10': 0.39; 'subject:" ': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'such': 0.63; 'decided': 0.64; 'more': 0.64; 'here': 0.66; 'natural': 0.68; 'subject': 0.69; 'url:a': 0.72; '2013,': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: "Natural" use of cmp= in sort Date: Mon, 10 Nov 2014 20:19:46 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bdacf3.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 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: 81 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1415647202 news.xs4all.nl 2878 [2001:888:2000:d::a6]:53380 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80478 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. > > > Here is a copy of my code: > > Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] > on win32 Type "copyright", "credits" or "license()" for more information. >>>> ineq = """f4 > f2 > f3 > f4 > f1 > f3 > f4 > f2 > f1 > f2 > f1 > f3""" >>>> print(ineq) > f4 > f2 > f3 > f4 > f1 > f3 > f4 > f2 > f1 > f2 > f1 > f3 >>>> greater_thans, all_f = set(), set() >>>> for line in ineq.split('\n'): > ....tokens = line.strip().split()[::2] > ....for n, t1 in enumerate(tokens[:-1]): > ........for t2 in tokens[n+1:]: > ............greater_thans.add((t1, t2)) > ............all_f.add(t1) > ........all_f.add(t2) > > >>>> sorted(all_f, cmp=lambda t1, t2: 0 if t1==t2 else > ...........(1 if (t1, t2) not in greater_thans else -1)) > ['f4', 'f2', 'f1', 'f3'] >>>> I'm not sure this works. I tried: $ cat paddy.py ineq = """f4 > f2 > f3 f4 > f1 > f3 f4 > f2 > f1 f2 > f1 > f3 f3 > f5 """ greater_thans = set() all_f = set() for line in ineq.split('\n'): tokens = line.strip().split()[::2] for n, t1 in enumerate(tokens[:-1]): for t2 in tokens[n+1:]: greater_thans.add((t1, t2)) all_f.add(t1) all_f.add(t2) print all_f print greater_thans print sorted(all_f, cmp=lambda t1, t2: 0 if t1==t2 else (1 if (t1, t2) not in greater_thans else -1)) $ PYTHONHASHSEED=0 python paddy.py set(['f1', 'f2', 'f3', 'f4', 'f5']) set([('f1', 'f3'), ('f2', 'f1'), ('f2', 'f3'), ('f4', 'f3'), ('f4', 'f2'), ('f4', 'f1'), ('f3', 'f5')]) ['f4', 'f2', 'f1', 'f3', 'f5'] $ PYTHONHASHSEED=1 python paddy.py set(['f5', 'f4', 'f3', 'f2', 'f1']) set([('f1', 'f3'), ('f2', 'f3'), ('f2', 'f1'), ('f4', 'f1'), ('f3', 'f5'), ('f4', 'f3'), ('f4', 'f2')]) ['f5', 'f4', 'f2', 'f1', 'f3']