Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'memory.': 0.05; 'try:': 0.07; 'python': 0.09; '-1.': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'subject:python': 0.11; '"positive': 0.16; '100,': 0.16; 'bisect': 0.16; 'radix': 0.16; 'sorting': 0.16; 'subject:3.3': 0.16; 'wrote:': 0.17; 'certainly': 0.17; 'memory': 0.18; 'suggested': 0.20; 'written': 0.20; 'sort': 0.21; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'first,': 0.27; 'subject:list': 0.28; 'run': 0.28; "d'aprano": 0.29; 'faster,': 0.29; 'steven': 0.29; 'writes:': 0.29; 'function': 0.30; 'expect': 0.31; 'lists': 0.31; 'running': 0.32; 'could': 0.32; 'zero': 0.33; 'received:google.com': 0.34; 'third': 0.34; 'list': 0.35; 'faster': 0.35; 'from:addr:googlemail.com': 0.35; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'really': 0.36; 'but': 0.36; 'compare': 0.36; 'thank': 0.36; 'best,': 0.37; 'two': 0.37; 'item': 0.37; 'received:209': 0.37; 'far': 0.37; 'subject:: ': 0.38; 'positive': 0.38; 'performance': 0.39; 'your': 0.60; 'easy': 0.60; 'range': 0.60; 'more': 0.63; 'as:': 0.75; '2013': 0.84; 'subject:Min': 0.84 X-Received: by 10.50.7.163 with SMTP id k3mr3662952iga.1.1363261530537; Thu, 14 Mar 2013 04:45:30 -0700 (PDT) Newsgroups: comp.lang.python Date: Thu, 14 Mar 2013 04:45:30 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=123.192.32.215; posting-account=5JdMBQoAAABHnS4mjpqEzxnmWtgiiVNw References: <513fdcfc$0$29965$c3e8da3$5496439d@news.astraweb.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 123.192.32.215 MIME-Version: 1.0 Subject: Re: Finding the Min for positive and negative in python 3.3 list From: 88888 Dihedral To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org 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: , Message-ID: Lines: 151 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1363261539 news.xs4all.nl 6901 [2001:888:2000:d::a6]:46350 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:41228 Wolfgang Maier=E6=96=BC 2013=E5=B9=B43=E6=9C=8813=E6=97=A5=E6=98=9F=E6=9C= =9F=E4=B8=89UTC+8=E4=B8=8B=E5=8D=886=E6=99=8243=E5=88=8638=E7=A7=92=E5=AF= =AB=E9=81=93=EF=BC=9A > Steven D'Aprano pearwood.info> writes: >=20 >=20 >=20 > >=20 >=20 > > On Tue, 12 Mar 2013 17:03:08 +0000, Norah Jones wrote: >=20 > >=20 >=20 > > > For example: >=20 > > > a=3D[-15,-30,-10,1,3,5] >=20 > > >=20 >=20 > > > I want to find a negative and a positive minimum. >=20 > > >=20 >=20 > > > example: negative >=20 > > > print(min(a)) =3D -30 >=20 > > > =20 >=20 > > > positive >=20 > > > print(min(a)) =3D 1 >=20 > >=20 >=20 > > Thank you for providing examples, but they don't really cover all the= =20 >=20 > > possibilities. For example, if you had: >=20 > >=20 >=20 > > a =3D [-1, -2, -3, 100, 200, 300] >=20 > >=20 >=20 > > I can see that you consider -3 to be the "negative minimum". Do you=20 >=20 > > consider the "positive minimum" to be 100, or 1? >=20 > >=20 >=20 > > If you expect it to be 100, then the solution is: >=20 > >=20 >=20 > > min([item for item in a if item > 0]) >=20 > >=20 >=20 > > If you expect it to be 1, then the solution is: >=20 > >=20 >=20 > > min([abs(item) for item in a]) >=20 > >=20 >=20 > > which could also be written as: >=20 > >=20 >=20 > > min(map(abs, a)) >=20 > >=20 >=20 > > A third alternative is in Python 3.3: >=20 > >=20 >=20 > > min(a, key=3Dabs) >=20 > >=20 >=20 > > which will return -1. >=20 > >=20 >=20 >=20 >=20 > thinking again about the question, then the min() solutions suggested so = far >=20 > certainly do the job and they are easy to understand. >=20 > However, if you need to run the function repeatedly on larger lists, usin= g min() >=20 > is suboptimal because its performance is an O(n) one. >=20 > It's faster, though less intuitive, to sort your list first, then use bis= ect on >=20 > it to find the zero position in it. Two manipulations running at O(log(n)= ). >=20 >=20 >=20 > compare these two functions: >=20 >=20 >=20 > def with_min(x): >=20 > return (min(n for n in a if n<0), min(n for n in a if n>=3D0)) >=20 >=20 >=20 > def with_bisect(x): >=20 > b=3Dsorted(x) >=20 > return (b[0] if b[0]<0 else None, b[bisect.bisect_left(b,0)]) >=20 >=20 >=20 > then either time them for small lists or try: >=20 >=20 >=20 > a=3Drange(-10000000,10000000) >=20 > with_min(a) >=20 > with_bisect(a) >=20 >=20 >=20 > of course, the disadvantage is that you create a huge sorted list in memo= ry and >=20 > that it's less readable. >=20 >=20 >=20 > Best, >=20 > Wolfgang Sorting numbers of such range M in a list of length N by radix sort=20 is faster but requires more memory.