Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"We don't have to protect the environment -- the Second Coming is at hand." -- James Watt


devel / comp.lang.python / Re: How to sort this without 'cmp=' in python 3?

SubjectAuthor
* Re: How to sort this without 'cmp=' in python 3?Mike H
`- Re: How to sort this without 'cmp=' in python 3?Chris Angelico

1
Re: How to sort this without 'cmp=' in python 3?

<831f18ce-eb79-48f8-8f4a-7f9033398988n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=24188&group=comp.lang.python#24188

  copy link   Newsgroups: comp.lang.python
X-Received: by 2002:a05:620a:1110:b0:773:eed7:76ad with SMTP id o16-20020a05620a111000b00773eed776admr230191qkk.11.1698195506236;
Tue, 24 Oct 2023 17:58:26 -0700 (PDT)
X-Received: by 2002:a05:6870:311e:b0:1e1:2ebc:b636 with SMTP id
v30-20020a056870311e00b001e12ebcb636mr6571533oaa.4.1698195505908; Tue, 24 Oct
2023 17:58:25 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border-2.nntp.ord.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.python
Date: Tue, 24 Oct 2023 17:58:25 -0700 (PDT)
In-Reply-To: <mailman.344.1476516444.30834.python-list@python.org>
Injection-Info: google-groups.googlegroups.com; posting-host=2603:8001:3041:903d:41ec:8cd8:4ba0:4d41;
posting-account=4ZEybwoAAADxVZfJ1SuJWs1TxuDcbbW6
NNTP-Posting-Host: 2603:8001:3041:903d:41ec:8cd8:4ba0:4d41
References: <716092e7-c9ab-4a8b-a157-c3c3357620fb@googlegroups.com>
<ntslnr$ij7$1@blaine.gmane.org> <mailman.344.1476516444.30834.python-list@python.org>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <831f18ce-eb79-48f8-8f4a-7f9033398988n@googlegroups.com>
Subject: Re: How to sort this without 'cmp=' in python 3?
From: mic...@gmail.com (Mike H)
Injection-Date: Wed, 25 Oct 2023 00:58:26 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 39
 by: Mike H - Wed, 25 Oct 2023 00:58 UTC

On Saturday, October 15, 2016 at 12:27:42 AM UTC-7, Peter Otten wrote:
> 38016...@gmail.com wrote:
>
> > nums=['3','30','34','32','9','5']
> > I need to sort the list in order to get the largest number string:
> > '953433230'
> >
> > nums.sort(cmp=lambda a,b: cmp(a+b, b+a), reverse=True)
> >
> > But how to do this in python 3?
> >
> > Thank you
> While cmp_to_key is neat doing it by hand should also be instructive.
> Essentially you move the comparison into a method of the key:
>
> $ cat translate_cmp.py
> class Key(str):
> def __lt__(a, b):
> return a + b < b + a
>
> nums = ['3','30','34','32','9','5']
> print(nums)
> nums.sort(key=Key, reverse=True)
> print(nums)
> print("".join(nums))
>
> $ python3 translate_cmp.py
> ['3', '30', '34', '32', '9', '5']
> ['9', '5', '34', '3', '32', '30']
> 953433230
>
> The above works because in CPython list.sort() currently uses only the <
> operator; adding __gt__() and __eq__() to make this portable is
> straightforward even if you do not use the functools.total_ordering class
> decorator.
Is it possible to use lambda expression instead of defining a `Key` class? Something like `sorted(my_list, key = lambda x, y: x+y > y+x)`?

Re: How to sort this without 'cmp=' in python 3?

<mailman.49.1698199934.3828.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=24193&group=comp.lang.python#24193

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ros...@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: How to sort this without 'cmp=' in python 3?
Date: Wed, 25 Oct 2023 13:12:01 +1100
Lines: 7
Message-ID: <mailman.49.1698199934.3828.python-list@python.org>
References: <716092e7-c9ab-4a8b-a157-c3c3357620fb@googlegroups.com>
<ntslnr$ij7$1@blaine.gmane.org>
<mailman.344.1476516444.30834.python-list@python.org>
<831f18ce-eb79-48f8-8f4a-7f9033398988n@googlegroups.com>
<CAPTjJmpOg_m0kRE3PkEk28QBVs7nhe53rj-ke83P4Kz8AVK4iQ@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de BpalUEKAQLag0YPjqFv3ng/AUIZloBYIMiSEXJ+VW9Cg==
Cancel-Lock: sha1:RA0KCUDPoJ7iKNN6eKmcI37MraI= sha256:2n5lbnCdKv0ywIChy4z1JY+5qWE2odgcMMrzt2Ri7XA=
Return-Path: <rosuav@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=g1ES8/jw;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.003
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:python': 0.06;
'2023': 0.07; 'subject:without': 0.07; 'cc:addr:python-list':
0.09; 'expression': 0.09; 'skip:` 10': 0.09; 'cc:no real
name:2**0': 0.14; 'chrisa': 0.16; 'from:addr:rosuav': 0.16;
'from:name:chris angelico': 0.16; 'lambda': 0.16; 'subject:sort':
0.16; 'wrote:': 0.16; 'instead': 0.17; 'cc:addr:python.org': 0.20;
'subject:How': 0.23; 'cc:2**0': 0.25; 'python-list': 0.32;
'message-id:@mail.gmail.com': 0.32; 'header:In-Reply-To:1': 0.34;
'received:google.com': 0.34; 'from:addr:gmail.com': 0.35; 'use':
0.39; 'wed,': 0.39; 'something': 0.40; 'key': 0.64; 'look': 0.65;
'subject:this': 0.67; 'skip:f 20': 0.75
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1698199933; x=1698804733; darn=python.org;
h=cc:to:subject:message-id:date:from:in-reply-to:references
:mime-version:from:to:cc:subject:date:message-id:reply-to;
bh=EqJRo2WQ7kga3MtuWLG/QYyNimZcBzwco+VYy+ET+nM=;
b=g1ES8/jwVHGx7N64XDPuoKzhJRTQO3DhsPLMVRs2VrqNfTO2Abd25y/LKspzFbw/B9
lYPtgqEXunxWeYdd1RI/EOQhFU/DTPIqHohd9t7fm3AgHmdPIj1Lv2nzxT2OsO1km3Ve
vr8IDOAKzmJ+C/ktD075xLfc5QgAyBXJU7OLsjopgSS40MBLgY0xWSA8/Qf4vRLlAQxy
OtwN60t3nGq6LHlZwhlqrgYvJ2PtEw0kE04CvAKn6Nx4aGbQg18YoeOGbrrp3hWaRbxT
uhLyaLxPCEcdrJhz5W3avY5/V5yzu3aR1SRKB5UsIF+/C8tFQMNFlsdld6kWCmKO5Xaj
1tUw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1698199933; x=1698804733;
h=cc:to:subject:message-id:date:from:in-reply-to:references
:mime-version:x-gm-message-state:from:to:cc:subject:date:message-id
:reply-to;
bh=EqJRo2WQ7kga3MtuWLG/QYyNimZcBzwco+VYy+ET+nM=;
b=YQS0szf5iL63UvwwZKlJSvDEiVsxIfyg0JuYzlfvOOK3HnJ08xMgZOpNU2CSkpkMOj
LbzXTOaDQy1enZA5NMheoZdYdEG3+OFd4naFFL5onyEY7QVRnnUlwt0XP1IMJjeFM2z7
gQUTre0yGFEOFg3rQLqqCAWYorU2cwYe46uKyvcflLBe/lUXkWcp6+carJ+394lE2rBD
zBxqvxrBJuU3r4dGYScWDP2ql8gxMPEVdc1v4ifj5y3WMirNsKePXPWllSmKoICuRX8X
81LVosJ7FY2aw9IjYlL+ACIR+/pICp0Rv6KUAwcbZTSLD+zATjfkMd4ZOxfHckNoELdm
CcOA==
X-Gm-Message-State: AOJu0YzgNT6Y2Gg4FIJMpxw9Nb07M8vuhiQUC3gO4o3FCAKzsMCwe1pB
7Prrpt2VM8QKckVfmufqnZaRjFu3HtY9ax+isGU=
X-Google-Smtp-Source: AGHT+IGGdHYdfXpk4YKkRb23noVtGvidDIbFo8Mj7/m1S4PLs/0W/Nnhoedm0peYCgt+0XYxdJBO4Gcz/u39rMYVtmA=
X-Received: by 2002:a05:6512:2211:b0:508:15dc:ebf8 with SMTP id
h17-20020a056512221100b0050815dcebf8mr666540lfu.38.1698199932769; Tue, 24 Oct
2023 19:12:12 -0700 (PDT)
In-Reply-To: <831f18ce-eb79-48f8-8f4a-7f9033398988n@googlegroups.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
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: <https://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: <CAPTjJmpOg_m0kRE3PkEk28QBVs7nhe53rj-ke83P4Kz8AVK4iQ@mail.gmail.com>
X-Mailman-Original-References: <716092e7-c9ab-4a8b-a157-c3c3357620fb@googlegroups.com>
<ntslnr$ij7$1@blaine.gmane.org>
<mailman.344.1476516444.30834.python-list@python.org>
<831f18ce-eb79-48f8-8f4a-7f9033398988n@googlegroups.com>
 by: Chris Angelico - Wed, 25 Oct 2023 02:12 UTC

On Wed, 25 Oct 2023 at 13:02, Mike H via Python-list
<python-list@python.org> wrote:
> Is it possible to use lambda expression instead of defining a `Key` class? Something like `sorted(my_list, key = lambda x, y: x+y > y+x)`?

Look up functools.cmp_to_key.

ChrisA

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor