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


Groups > comp.lang.python > #41146

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

From Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
Subject Re: Finding the Min for positive and negative in python 3.3 list
Date 2013-03-12 18:13 +0000
References <abcd1234abc123ab12a0000000027000020000001052@gmail.com> <loom.20130312T184238-109@post.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.3247.1363112013.2939.python-list@python.org> (permalink)

Show all headers | View raw


Wolfgang Maier <wolfgang.maier <at> biologie.uni-freiburg.de> writes:

> 
> Norah Jones <nh.jones01 <at> gmail.com> writes:
> 
> > 
> > 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
> > 
> > 
> > 
> 
> try this:
> min(a)   => -30
> min([n for n in a if n>0])   => 1
> 
> of course, you have to figure out what you want to do with a zero value.
> 
> 

the i above has to be an n, of course, sorry for that typo.

by the way, if you need both values and your list is really huge, an explicit
for loop checking each number whether it's the current negative and positive
minimum might be faster, but that would have to be tested. Also, I'm wondering
whether you could somehow exploit the fact that if your list contains 0 (or 1
depending on how you want to treat zero values) you have for sure found the
minimum for your positive numbers? 




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


Thread

Re: Finding the Min for positive and negative in python 3.3 list Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-03-12 18:13 +0000

csiph-web