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


Groups > comp.lang.python > #41168

Re: Finding the Min for positive and negative in python 3.3 list

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'example:': 0.03; 'none,': 0.05; 'try:': 0.07; 'python': 0.09; '-1.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.10; 'subject:python': 0.11; '"positive': 0.16; '100,': 0.16; 'bisect': 0.16; 'message- id:@post.gmane.org': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 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; 'header:User-Agent:1': 0.26; 'first,': 0.27; 'header:X-Complaints-To:1': 0.28; '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; 'to:addr :python-list': 0.33; 'skip:b 20': 0.34; 'third': 0.34; 'list': 0.35; 'received:org': 0.36; 'really': 0.36; 'but': 0.36; 'compare': 0.36; 'thank': 0.36; 'charset:us-ascii': 0.36; 'best,': 0.37; 'two': 0.37; 'item': 0.37; 'far': 0.37; 'subject:: ': 0.38; 'positive': 0.38; 'performance': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'easy': 0.60; 'as:': 0.75; '2013': 0.84; 'subject:Min': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
Subject Re: Finding the Min for positive and negative in python 3.3 list
Date Wed, 13 Mar 2013 10:43:38 +0000 (UTC)
References <mailman.3239.1363109618.2939.python-list@python.org> <513fdcfc$0$29965$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host sea.gmane.org
User-Agent Loom/3.14 (http://gmane.org/)
X-Loom-IP 192.52.35.172 (Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0)
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3258.1363171436.2939.python-list@python.org> (permalink)
Lines 71
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1363171436 news.xs4all.nl 6913 [2001:888:2000:d::a6]:56320
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:41168

Show key headers only | View raw


Steven D'Aprano <steve+comp.lang.python <at> pearwood.info> writes:

> 
> On Tue, 12 Mar 2013 17:03:08 +0000, Norah Jones wrote:
> 
> > For example:
> > a=[-15,-30,-10,1,3,5]
> > 
> > I want to find a negative and a positive minimum.
> > 
> > example: negative
> > print(min(a)) = -30
> >  
> > positive
> > print(min(a)) = 1
> 
> Thank you for providing examples, but they don't really cover all the 
> possibilities. For example, if you had:
> 
> a = [-1, -2, -3, 100, 200, 300]
> 
> I can see that you consider -3 to be the "negative minimum". Do you 
> consider the "positive minimum" to be 100, or 1?
> 
> If you expect it to be 100, then the solution is:
> 
>     min([item for item in a if item > 0])
> 
> If you expect it to be 1, then the solution is:
> 
>     min([abs(item) for item in a])
> 
> which could also be written as:
> 
>     min(map(abs, a))
> 
> A third alternative is in Python 3.3:
> 
>     min(a, key=abs)
> 
> which will return -1.
> 

thinking again about the question, then the min() solutions suggested so far
certainly do the job and they are easy to understand.
However, if you need to run the function repeatedly on larger lists, using min()
is suboptimal because its performance is an O(n) one.
It's faster, though less intuitive, to sort your list first, then use bisect on
it to find the zero position in it. Two manipulations running at O(log(n)).

compare these two functions:

def with_min(x):
    return (min(n for n in a if n<0), min(n for n in a if n>=0))

def with_bisect(x):
    b=sorted(x)
    return (b[0] if b[0]<0 else None, b[bisect.bisect_left(b,0)])

then either time them for small lists or try:

a=range(-10000000,10000000)
with_min(a)
with_bisect(a)

of course, the disadvantage is that you create a huge sorted list in memory and
that it's less readable.

Best,
Wolfgang

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


Thread

Finding the Min for positive and negative in python 3.3 list Norah Jones <nh.jones01@gmail.com> - 2013-03-12 17:03 +0000
  Re: Finding the Min for positive and negative in python 3.3 list Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-13 01:57 +0000
    Re: Finding the Min for positive and negative in python 3.3 list Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-03-13 10:43 +0000
      Re: Finding the Min for positive and negative in python 3.3 list 88888 Dihedral <dihedral88888@googlemail.com> - 2013-03-14 04:45 -0700
      Re: Finding the Min for positive and negative in python 3.3 list 88888 Dihedral <dihedral88888@googlemail.com> - 2013-03-14 04:45 -0700
    Re: Finding the Min for positive and negative in python 3.3 list Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-03-13 11:23 +0000
      Re: Finding the Min for positive and negative in python 3.3 list Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-13 14:12 +0000
        Re: Finding the Min for positive and negative in python 3.3 list Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-03-13 14:37 +0000
    Re: Finding the Min for positive and negative in python 3.3 list Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-03-13 11:34 +0000
      Re: Finding the Min for positive and negative in python 3.3 list Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-13 14:43 +0000
    Re: Finding the Min for positive and negative in python 3.3 list Chris Angelico <rosuav@gmail.com> - 2013-03-13 22:36 +1100
    Re: Finding the Min for positive and negative in python 3.3 list Chris Angelico <rosuav@gmail.com> - 2013-03-13 22:38 +1100
    Re: Finding the Min for positive and negative in python 3.3 list Peter Otten <__peter__@web.de> - 2013-03-13 13:00 +0100
    Re: Finding the Min for positive and negative in python 3.3 list Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-03-13 14:17 +0000

csiph-web