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


Groups > comp.lang.python > #106685

Re: how to convert code that uses cmp to python3

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: how to convert code that uses cmp to python3
Date Fri, 8 Apr 2016 08:22:46 -0600
Lines 40
Message-ID <mailman.88.1460125409.2253.python-list@python.org> (permalink)
References <57064D0D.1030701@rece.vub.ac.be> <CAPTjJmrGiQS9uhFU-+TQ4nUWQ2p4gmJoQitMotS0Ob--qm1iWg@mail.gmail.com> <5706C961.2000009@rece.vub.ac.be> <CAPTjJmrbL870mV1kU8nHar=bPyKZRmhKP-8iUs_tpYVo5vhhOA@mail.gmail.com> <57075F43.7060004@rece.vub.ac.be> <85fuuw5ypl.fsf@benfinney.id.au> <5707B2CE.1010407@rece.vub.ac.be> <CAPTjJmrMv5O5vwrbfHJJCdiHi87naPOanhPmk4Ba_SLRgu1m4Q@mail.gmail.com> <CALwzid=U5BWiLB2Z+aXS6vd3nPM0rvSUrVgYt4f089L5nEfgNQ@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de sa8mabXUkF6Is4+snkFykgQ3gYtRD48bNmLl3W5D+euA==
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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'received:209.85.223': 0.03; 'elif': 0.04; 'none:': 0.05; 'paths': 0.05; 'subject:code': 0.07; 'python': 0.10; 'def': 0.13; 'result.': 0.15; '2016': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'seconds.': 0.16; 'subject:python3': 0.16; 'wrote:': 0.16; 'comparing': 0.18; 'delta': 0.22; 'am,': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'chris': 0.26; 'compare': 0.27; 'equivalent': 0.27; 'fri,': 0.27; 'message- id:@mail.gmail.com': 0.27; 'node': 0.29; 'subject:that': 0.29; 'raise': 0.29; 'code': 0.30; 'version,': 0.30; 'implement': 0.32; 'received:google.com': 0.35; 'follows:': 0.35; 'there': 0.36; 'received:209.85': 0.36; 'possible': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'doing': 0.38; 'received:209': 0.38; 'why': 0.39; 'takes': 0.39; 'to:addr:python.org': 0.40; 'more': 0.63; 'directions,': 0.84; 'pardon': 0.84; 'to:name:python': 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; bh=ytx4HkXxQuvi00tXeGmPodVGn45XOMAt562LJSgDy6I=; b=Ao29bfscxuWhLznq4IYMCJRXz75goSvoAp+B2L/qZ0R9O/nE2Rhm2a14BNq/WDSatD 3gY+7uudN//bfV9Imttdn5yp5S5DkGKuu9dWJ3yHrwft070A/TqDAj0y0jVBCeMUvh0E Dru3esC7hzdic9atKucCOLk3OsgMgMU5hB344j8QrGQPQyQsdsbjbhhyUScC0N7bheoj 8ax2ITbnqlI8HuFdWO+ZmBfe9zAis/xGhaL9H+AsSMR+4JZzoTaaNhcWSntpAeqXg/fc gwQ9rlSHCU7FWVJG+DSAj7I7hy9N/rWdN6PDQb8cAXonhlzpCZOdAyiaVqntZ5XO2KEG 74Yw==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=ytx4HkXxQuvi00tXeGmPodVGn45XOMAt562LJSgDy6I=; b=IkdNvjEoA7/q7edn/mfWpiKN25YxAEcecrAkAuqhZ+wHJe4zA+0vP/TY/A4g1tgg9J IX3eqf3QmG/XODPhtz3rr5yASRUdc2TcsQyXx95uPx+qrDXe6G/JIKV5s6tEoVespHow 2TKu9iKsxFkXxvHKEGxoRWDi2fDIO7Qcwi2lL70462LPvdELsilTCD31TDRLzpvO4fDV GMfUAgZfj4jL/Vwm0hilfn4BCMdWWCoJiIBOpLwlh2Ra3S1XRLtNosddbk6uyRcYYHKD /RkhxdEcu4zj+yO5RnwZaUkJrwMGkTW4BPiPe5zUdzqYeZ9PVjET6jQvbK+X9kWejQm9 H5jw==
X-Gm-Message-State AD7BkJIlk1+44B8anKsaFitmWivLiutAtNdpUaIbXLDFbebI7KUwqJf5AMK77meHGKNoeH14dlnlXb2eStPu8g==
X-Received by 10.107.10.87 with SMTP id u84mr9380418ioi.188.1460125406332; Fri, 08 Apr 2016 07:23:26 -0700 (PDT)
In-Reply-To <CAPTjJmrMv5O5vwrbfHJJCdiHi87naPOanhPmk4Ba_SLRgu1m4Q@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
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>
X-Mailman-Original-Message-ID <CALwzid=U5BWiLB2Z+aXS6vd3nPM0rvSUrVgYt4f089L5nEfgNQ@mail.gmail.com>
X-Mailman-Original-References <57064D0D.1030701@rece.vub.ac.be> <CAPTjJmrGiQS9uhFU-+TQ4nUWQ2p4gmJoQitMotS0Ob--qm1iWg@mail.gmail.com> <5706C961.2000009@rece.vub.ac.be> <CAPTjJmrbL870mV1kU8nHar=bPyKZRmhKP-8iUs_tpYVo5vhhOA@mail.gmail.com> <57075F43.7060004@rece.vub.ac.be> <85fuuw5ypl.fsf@benfinney.id.au> <5707B2CE.1010407@rece.vub.ac.be> <CAPTjJmrMv5O5vwrbfHJJCdiHi87naPOanhPmk4Ba_SLRgu1m4Q@mail.gmail.com>
Xref csiph.com comp.lang.python:106685

Show key headers only | View raw


On Fri, Apr 8, 2016 at 8:08 AM, Chris Angelico <rosuav@gmail.com> wrote:
> On Fri, Apr 8, 2016 at 11:31 PM, Antoon Pardon
> <antoon.pardon@rece.vub.ac.be> wrote:
>> Doing it as follows:
>>     seq1 < seq2
>>     seq2 < seq1
>>
>> takes about 110 seconds.
>>
>>
>> Doing it like this:
>>     delta = cmp(seq1, seq2)
>>     delta < 0
>>     delta > 0
>>
>> takes about 50 seconds.
>
> Why are you comparing in both directions, though? cmp() is more
> equivalent to this:
>
> seq1 == seq2
> seq1 < seq2
>
> You only need ONE comparison, and the other is presumed to be its
> opposite. When, in the Python 3 version, would you need to compare
> twice?

When there are three possible code paths depending on the result.

def search(key, node):
    if node is None:
        raise KeyError(key)
    if key < node.key:
        return search(key, node.left)
    elif key == node.key:
        return node
    else:
        return search(key, node.right)

How would you implement this with only one comparison?

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


Thread

Re: how to convert code that uses cmp to python3 Ian Kelly <ian.g.kelly@gmail.com> - 2016-04-08 08:22 -0600

csiph-web